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編碼格式即會正常,不會出現亂碼的情況