使用Delphi调用Http GET url的最简单的方法是什么? [英] What's the simplest way to call Http GET url using Delphi?

查看:347
本文介绍了使用Delphi调用Http GET url的最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中调用一个Web服务,我可以使用它导入WSDL,或者只需使用HTTP GET与URL和参数,所以我更喜欢稍后,因为这很简单。 >

我知道我可以使用indy idhttp.get来做这个工作,但这是非常简单的事情,我不想在我的应用程序中添加复杂的indy代码。 p>

更新:抱歉如果我不清楚,我的意思是不要添加复杂的indy代码,我不想添加indy组件对于这个简单的任务,并且更喜欢更轻的方式。

解决方案

你可以使用 WinINet API ,如下所示:

 使用WinInet; 

函数GetUrlContent(const Url:string):string;
var
NetHandle:HINTERNET;
UrlHandle:HINTERNET;
缓冲区:数组[0..1024]的Char;
BytesRead:dWord;
begin
结果:='';
NetHandle:= InternetOpen('Delphi 5.x',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);

如果分配(NetHandle)然后
begin
UrlHandle:= InternetOpenUrl(NetHandle,PChar(Url),nil,0,INTERNET_FLAG_RELOAD,0);

如果分配(UrlHandle)然后
{UrlHandle有效?继续下载}
begin
FillChar(Buffer,SizeOf(Buffer),0);
repeat
结果:= Result + Buffer;
FillChar(Buffer,SizeOf(Buffer),0);
InternetReadFile(UrlHandle,@Buffer,SizeOf(Buffer),BytesRead);
,直到BytesRead = 0;
InternetCloseHandle(UrlHandle);
end
else
{UrlHandle无效。提起例外}
raise Exception.CreateFmt('无法打开URL%s',[Url]);

InternetCloseHandle(NetHandle);
end
else
{NetHandle无效。提出异常}
raise Exception.Create('无法初始化Wininet');
结束

来源: http://www.scalabium.com/faq/dct0080.htm



WinINet API使用相同的东西InternetExplorer正在使用,所以您还可以免费获取InternetExplorer设置的任何连接和代理设置。


There's a web services I want to call in my application, I can use it with importing the WSDL or by just use "HTTP GET" with the URL and parameters, so I prefer the later because it's simple thing.

I know I can use indy idhttp.get, to do the job, but this is very simple thing and I don't want to add complex indy code to my application.

UPDATE: sorry if I was not clear, I meant by "not to add complex indy code", that I don't want add indy components for just this simple task, and prefer more lighter way for that.

解决方案

You could use the WinINet API like this:

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;

source: http://www.scalabium.com/faq/dct0080.htm

The WinINet API uses the same stuff InternetExplorer is using so you also get any connection and proxy settings set by InternetExplorer for free.

这篇关于使用Delphi调用Http GET url的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆