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

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

问题描述

我想在我的应用程序中调用一个 Web 服务,我可以通过导入 WSDL 或仅使用带有 URL 和参数的HTTP GET"来使用它,所以我更喜欢后者,因为它很简单.

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.

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

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.

更新:对不起,如果我不清楚,我的意思是不要添加复杂的 indy 代码",我不想为这个简单的任务添加 indy 组件,并且更喜欢更轻的方法.

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.

推荐答案

您可以使用 WinINet API 像这样:

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;

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

WinINet API 使用 InternetExplorer 使用的相同内容,因此您还可以免费获得 InternetExplorer 设置的任何连接和代理设置.

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天全站免登陆