idhttp.get(AURL:string):string;//返回字符串
idhttp.get(AURL:string;AResponseContent: TStream)//返回流
基于网站的不同编码格式,第二种返回流的更加适用
1 function httpGetByStr(Url: string): string; 2 begin 3 FIdhttp := TIdHTTP.Create(nil); 4 try 5 FIdhttp.ConnectTimeout := 3000; 6 FIdhttp.ReadTimeout := 6000; 7 Result := FIdhttp.Get(Url); 8 finally 9 FIdhttp.Disconnect; 10 FreeAndNil(FIdhttp); 11 end; 12 end;
1 function TSearchLrcThread.httpGetByStream(Url: string; AEncoding: TEncoding) 2 : TStringStream; 3 begin 4 Result := TStringStream.Create('', AEncoding); 5 FIdhttp := TIdHTTP.Create(nil); 6 try 7 FIdhttp.ConnectTimeout := 3000; 8 FIdhttp.ReadTimeout := 6000; 9 FIdhttp.Get(FUrl, Result); 10 finally 11 FIdhttp.Disconnect; 12 FreeAndNil(FIdhttp); 13 end; 14 end;
基于Unicode版本,根据网站的不同编码设置不同的AEncoding.类型即可,返回的Datastring编码格式即会正常,不会出现乱码的情况