function GetUrlContent(const Url: string): string; var NetHandle: HINTERNET; UrlHandle: HINTERNET; Buffer: array[0..1024] of Char; BytesRead: dWord; begin Result := ''; NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(NetHandle) then begin UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle) then { UrlHandle valid? Proceed with download } begin FillChar(Buffer, SizeOf(Buffer), 0); repeat Result := Result + Buffer; FillChar(Buffer, SizeOf(Buffer), 0); InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); until BytesRead = 0; InternetCloseHandle(UrlHandle); end else { UrlHandle is not valid. Raise an exception. } raise Exception.CreateFmt('Cannot open URL %s', [Url]);
InternetCloseHandle(NetHandle); end else { NetHandle is not valid. Raise an exception } raise Exception.Create('Unable to initialize'); end;
我新写了一个 HttpClient 实现现已经正常,谢谢大家回复, 结贴了。 大概代码是这样 get TStringStream
var ss:TStringStream; begin Result:=''; ss:=TStringStream.Create; try Get(URL,SS); Result:=ss.DataString; finally ss.Free; end;
request
P:=FHttp.ResStream ; W:=VarType(P); if w = varUnknown then begin iu:=IUnknown(P); iu.QueryInterface(IStream_GUID,s); end; if s=nil then Exit; res.Position:=0; os:=TOleStream.Create(s); try os.Position:=0; res.CopyFrom(os,os.Size) finally os.Free end;
----------------------------------------------
久吧天空,每天必上的一个网站 http://www.98sky.com