我想在我的应用程序中调用一个Web服务,我可以在导入WSDL时使用它,或者只使用带有URL和参数的"HTTP GET",所以我更喜欢后者,因为它很简单.
我知道我可以使用indy idhttp.get来完成这项工作,但这很简单,我不想在我的应用程序中添加复杂的indy代码.
更新:抱歉,如果我不清楚,我的意思是"不要添加复杂的indy代码",我不想为这个简单的任务添加indy组件,并且更喜欢更轻松的方式.
使用Indy调用RESTful Web服务非常简单.
将IdHTTP添加到您的uses子句中.请记住,IdHTTP需要URL上的"HTTP://"前缀.
function GetURLAsString(const aURL: string): string; var lHTTP: TIdHTTP; begin lHTTP := TIdHTTP.Create; try Result := lHTTP.Get(aURL); finally lHTTP.Free; end; end;
您可以像这样使用WinINet API:
uses WinInet; 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 Wininet'); end;
来源:http://www.scalabium.com/faq/dct0080.htm
WinINet API使用InternetExplorer使用的相同内容,因此您还可以免费获得InternetExplorer设置的任何连接和代理设置.
实际上接受的答案中的代码对我不起作用.所以我稍微修改了它,所以它实际上返回String并在执行后优雅地关闭所有内容.示例将检索到的数据作为UTF8String返回,因此它适用于ASCII和UTF8页面.
uses WinInet; function GetUrlContent(const Url: string): UTF8String; var NetHandle: HINTERNET; UrlHandle: HINTERNET; Buffer: array[0..1023] of byte; BytesRead: dWord; StrBuffer: UTF8String; begin Result := ''; NetHandle := InternetOpen('Delphi 2009', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); if Assigned(NetHandle) then try UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0); if Assigned(UrlHandle) then try repeat InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead); Result := Result + StrBuffer; until BytesRead = 0; finally InternetCloseHandle(UrlHandle); end else raise Exception.CreateFmt('Cannot open URL %s', [Url]); finally InternetCloseHandle(NetHandle); end else raise Exception.Create('Unable to initialize Wininet'); end;
希望对像我这样寻找简单代码如何在Delphi中检索页面内容的人有所帮助.干杯,阿尔迪斯:)
如果可以下载到文件,您可以使用ExtActns单元中的TDownloadURL.比直接使用WinInet简单得多.
procedure TMainForm.DownloadFile(URL: string; Dest: string); var dl: TDownloadURL; begin dl := TDownloadURL.Create(self); try dl.URL := URL; dl.FileName := Dest; dl.ExecuteTarget(nil); //this downloads the file finally dl.Free; end; end;
使用它时也可以获得进度通知.只需将事件处理程序分配给TDownloadURL的OnDownloadProgress事件即可.
在新德尔福版本中,它是更好地使用THTTPClient
从System.Net.HttpClient
单元,因为它是标准的,跨平台的.简单的例子是
function GetURL(const AURL: string): string; var HttpClient: THttpClient; HttpResponse: IHttpResponse; begin HttpClient := THTTPClient.Create; try HttpResponse := HttpClient.Get(AURL); Result := HttpResponse.ContentAsString(); finally HttpClient.Free; end; end;