首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何异步使用"WinHttp.WinHttpRequest.5.1“?

如何异步使用"WinHttp.WinHttpRequest.5.1“?
EN

Stack Overflow用户
提问于 2011-12-17 19:01:33
回答 3查看 12.1K关注 0票数 4

守则:

代码语言:javascript
复制
var
  WinHttpReq: OleVariant;

procedure TForm1.Button1Click(Sender: TObject);    
begin
  WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
  WinHttpReq.Open('GET', 'http://stackoverflow.com', TRUE); // asynchronously
  WinHttpReq.setRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
  WinHttpReq.Send();
  // HOW to set a callback procedure here and get the response?
end;

注意:我不想导入mshttp.dll并使用TLB。我想通过后期装订来使用它。如果有例外情况,我也会处理。

编辑:,我接受喇嘛的回答,因为它给了我一个很好的选择,我最初的要求。此外,它有一个很好的例子来源。

这里是事件的WinHTTPRequest包装器( IConnectionPoint )的一个非常好的实现(源代码附后)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-18 00:11:46

正如Stijn在他的回答中所说,为了防止程序滞后,请使用线程。IWinHttpRequest.Open也具有异步配置功能,但是很难捕捉到事件,而且IWinHttpRequest.WaitForResponse也会将您的程序卡住。

下面是如何将响应文本放入表单的备注框的简单示例。请注意,下面的示例使用同步模式,您还可以使用IWinHttpRequest.SetTimeouts修改超时值。如果您想像在问题中一样使用异步模式,那么您必须等待IWinHttpRequest.WaitForResponse方法的结果。

代码语言:javascript
复制
///////////////////////////////////////////////////////////////////////////////
/////   WinHttpRequest threading demo unit   //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

unit WinHttpRequestUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActiveX, ComObj, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

///////////////////////////////////////////////////////////////////////////////
/////   THTTPRequest - TThread descendant for single request   ////////////////
///////////////////////////////////////////////////////////////////////////////

type
  THTTPRequest = class(TThread)
  private
    FRequestURL: string;
    FResponseText: string;
    procedure Execute; override;
    procedure SynchronizeResult;
  public
    constructor Create(const RequestURL: string);
    destructor Destroy; override;
  end;

///////////////////////////////////////////////////////////////////////////////
/////   THTTPRequest.Create - thread constructor   ////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// RequestURL - the requested URL

constructor THTTPRequest.Create(const RequestURL: string);
begin
  // create and start the thread after create
  inherited Create(False);
  // free the thread after THTTPRequest.Execute returns
  FreeOnTerminate := True;
  // store the passed parameter into the field for future use
  FRequestURL := RequestURL;
end;

///////////////////////////////////////////////////////////////////////////////
/////   THTTPRequest.Destroy - thread destructor   ////////////////////////////
///////////////////////////////////////////////////////////////////////////////

destructor THTTPRequest.Destroy;
begin
  inherited;
end;

///////////////////////////////////////////////////////////////////////////////
/////   THTTPRequest.Execute - thread body   //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

procedure THTTPRequest.Execute;
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', FRequestURL, 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;

///////////////////////////////////////////////////////////////////////////////
/////   THTTPRequest.SynchronizeResult - synchronization method   /////////////
///////////////////////////////////////////////////////////////////////////////

procedure THTTPRequest.SynchronizeResult;
begin
  // because of calling this method through Synchronize it is safe to access
  // the VCL controls from the main thread here, so let's fill the memo text
  // with the HTTP response stored before
  Form1.Memo1.Lines.Text := FResponseText;
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.Button1Click - button click event   ////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// Sender - object which invoked the event

procedure TForm1.Button1Click(Sender: TObject);
begin
  // because the thread will be destroyed immediately after the Execute method
  // finishes (it's because FreeOnTerminate is set to True) and because we are
  // not reading any values from the thread (it fills the memo box with the
  // response for us in SynchronizeResult method) we don't need to store its
  // object instance anywhere as well as we don't need to care about freeing it
  THTTPRequest.Create('http://stackoverflow.com');
end;

end.
票数 3
EN

Stack Overflow用户

发布于 2013-07-03 00:44:23

IWinHttpRequest是相当原始的。请注意使用Open()中指定的异步模式!

如果您认为可以使用由IStream ()返回的IStream下载一个大文件,并在数据到达时以小块的形式将数据写回文件,那么您就错了。

不管您使用的是同步模式还是异步模式: IWinHttpRequest总是将整个服务器响应加载到内存中,get_ResponseStream()返回E_PENDING,直到整个下载都存储在内存中。

此接口仅为小文件设计。

票数 2
EN

Stack Overflow用户

发布于 2011-12-17 21:37:49

我建议您了解TThread对象。创建一个继承自TThread的新类,重写Execute方法,调用CoInitialize (以启用COM)并执行WinHTTPRequest代码。当请求完成后,使用Synchronize将结果传递回前台线程。此外,您还应该能够在Execute方法中的try/ the子句中捕获异常。

另一个选项是切换到具有异步布尔属性的IXMLHTTPRequest对象。捕获带有后期绑定的事件可能非常困难,但您可以定期检查状态属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8547188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档