使用Delphi调用Http GET url的最简单方法是什么?

Moh*_*man 43 delphi network-programming http

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

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

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

Bru*_*Gee 34

使用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;
Run Code Online (Sandbox Code Playgroud)

  • 问题是不添加复杂的Indy代码.代码并不复杂. (5认同)
  • 在它的辩护中,Indy随Delphi一起发布,但WinInet版本和行为随着安装的IE版本而改变,这就像DLL地狱一样. (4认同)
  • 问题是如果没有Indy可以做到 (3认同)

Lar*_*ens 27

您可以像这样使用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;
Run Code Online (Sandbox Code Playgroud)

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

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

  • 请注意,不应在服务中使用WinInet. (3认同)

Ald*_*dis 16

实际上接受的答案中的代码对我不起作用.所以我稍微修改了它,所以它实际上返回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;
Run Code Online (Sandbox Code Playgroud)

希望对像我这样寻找简单代码如何在Delphi中检索页面内容的人有所帮助.干杯,阿尔迪斯:)


Mic*_*sen 7

如果可以下载到文件,您可以使用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;
Run Code Online (Sandbox Code Playgroud)

使用它时也可以获得进度通知.只需将事件处理程序分配给TDownloadURL的OnDownloadProgress事件即可.


Eug*_*neK 7

在新德尔福版本中,它是更好地使用THTTPClientSystem.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;
Run Code Online (Sandbox Code Playgroud)


Ari*_*The 5

使用 Windows HTTP API 也可能很容易。

procedure TForm1.Button1Click(Sender: TObject);
var http: variant;
begin
 http:=createoleobject('WinHttp.WinHttpRequest.5.1');
 http.open('GET', 'http://lazarus.freepascal.org', false);
 http.send;
 showmessage(http.responsetext);
end;
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我暗示已经为主 VCL 线程初始化了 COM。据报道,简单的应用程序或拼箱应用程序可能并非总是如此。此外,异步(多线程)工作绝对不是这种情况。

下面是一个真实代码运行的片段。注意 - 功能是奖励。它不需要工作。因此,虽然我确实发出请求,但我并不关心他们的结果,该结果会被忽略并丢弃。

procedure TfmHaspList.YieldBlinkHTTP(const LED: boolean; const Key_Hardware_ID: cardinal);
var URL: WideString;
begin
  URL := 'http://127.0.0.1:1947/action.html?blink' +
    IfThen( LED, 'on', 'off') + '=' + IntToStr(Key_Hardware_ID);

  TThread.CreateAnonymousThread(
    procedure
    var Request: OleVariant;
    begin
      // COM library initialization for the current thread
      CoInitialize(nil);
      try
        // create the WinHttpRequest object instance
        Request := CreateOleObject('WinHttp.WinHttpRequest.5.1');
        // open HTTP connection with GET method in synchronous mode
        Request.Open('GET', URL, False);
        // set the User-Agent header value
//        Request.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
        // sends the HTTP request to the server, the Send method does not return
        // until WinHTTP completely receives the response (synchronous mode)
        Request.Send;
//        // store the response into the field for synchronization
//        FResponseText := Request.ResponseText;
//        // execute the SynchronizeResult method within the main thread context
//        Synchronize(SynchronizeResult);
      finally
        // release the WinHttpRequest object instance
        Request := Unassigned;
        // uninitialize COM library with all resources
        CoUninitialize;
      end;
    end
  ).Start;
end;
Run Code Online (Sandbox Code Playgroud)

  • 异步线程案例的链接中提到了这一点。然而,在这个特定的例子中,对象在主 VCL 线程中运行,其中 COM 已经被初始化。我想(虽然没有检查)同样适用于 Windows 上的 LCL (2认同)