首页 > 代码库 > 一、酷狗 歌词搜索 Indy TIdhttp

一、酷狗 歌词搜索 Indy TIdhttp

酷狗歌词Url: http://lib9.service.kugou.com/websearch/index.php?page=1&cmd=100&pagesize=9&keyword= +歌名

酷狗歌词编码:UTF-8

中文歌名转UTF8编码代码

 1 function StrToHex(str: string; AEncoding: TEncoding): string; 2 var 3   ss: TStringStream; 4   i: Integer; 5 begin 6   Result := ‘‘; 7   ss := TStringStream.Create(str, AEncoding); 8   try 9     for i := 0 to ss.Size - 1 do10       Result := Result + % + Format(%.2x, [ss.Bytes[i]]);11   finally12     ss.Free;13   end;14 end;

StrToHex(歌词,TEncoding.utf8)

TIdhttp调用代码

 1   function httpGetByStream(Url:string;AEncoding:TEncoding):TStringStream; 2   begin 3    try 4     Result := TStringStream.Create(‘‘, AEncoding); 5     FIdhttp := TIdHTTP.Create(nil); 6     FIdhttp.ConnectTimeout := 3000; 7     FIdhttp.ReadTimeout := 6000; 8     FIdhttp.Get(FUrl, Result); 9    finally10      FIdhttp.Disconnect;11      FreeAndNil(FIdhttp);12      end;13   end;14 15 var16   ReStreamBuff : TStringStream;17 begin18        //以Utf8编码格式实例化字符串流,19         ReStreamBuff := TStringStream.Create(‘‘, TEncoding.UTF8);20         try21           ReStreamBuff := httpGetByStream(FUrl, TEncoding.UTF8);22          //do   datasting;23         finally24           ReStreamBuff.Free;25         end;26 end;

最后解析出来的ReStreamBuff为字符串流,其Datastring为JSON串

使用系统自带的System.JSON类解析即可

 

一、酷狗 歌词搜索 Indy TIdhttp