Multithreading 如何使用;WinHttp.WinHttpRequest.5.1“;异步?
守则:Multithreading 如何使用;WinHttp.WinHttpRequest.5.1“;异步?,multithreading,delphi,asynchronous,winhttp,Multithreading,Delphi,Asynchronous,Winhttp,守则: var WinHttpReq: OleVariant; procedure TForm1.Button1Click(Sender: TObject); begin WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1'); WinHttpReq.Open('GET', 'http://stackoverflow.com', TRUE); // asynchronously WinHttpReq.set
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。我想通过后期绑定使用它。我还想处理例外情况,如果有的话
编辑:
我接受特拉玛的答案,因为它给了我一个很好的选择,以取代我最初的要求。另外,它还有一个很好的示例源代码
下面是的一个非常好的实现(附带源代码) 我建议您了解TThread对象。创建一个从TThread继承的新类,重写Execute方法,调用CoInitialize(以启用COM)并执行WinHTTPRequest代码。请求完成后,使用Synchronize将结果传递回前台线程。您还应该能够在Execute方法的try/except子句中捕获异常
另一个选项是切换到IXMLHTTPRequest对象,该对象具有异步布尔属性。用延迟绑定捕获事件可能非常困难,但您可以定期检查state属性。正如Stijn在回答中所说,为了防止程序延迟,请使用线程。也具有异步配置功能,但捕获事件将非常困难,即使如此也会使程序陷入困境 下面是一个简单的示例,说明如何将响应文本放入表单的备注框中。 请注意,以下示例使用同步模式,您还可以使用修改超时值。如果您想像在问题中一样使用异步模式,那么必须使用方法等待结果
我的请求很原始。 使用Open()中指定的异步模式进行警告 如果您认为可以使用get_ResponseStream()返回的IStream下载一个大文件,并在文件到达时将数据以小块的形式写回文件,那么您就错了 无论使用同步还是异步模式:iwinhtprequest始终将整个服务器响应加载到内存中,get_ResponseStream()返回E_PENDING,直到整个下载存储到内存中
此界面仅为小文件设计。非常好的代码TLama。但是,我想实现
OnResponseDataAvailable
,OnError
等等。顺便说一句,如果代码在主线程中运行,我们是否需要共同初始化
?如果使用COM后期绑定,那么实现事件处理就相当困难了。是的,您必须调用,因为我的示例中的THTTPRequest
是工作线程(TThread
后代),而不是主线程(虽然它们在同一个单元中,但是假设TForm1
是主线程,每个THTTPRequest
是独立的工作线程)。您必须为每个线程初始化COM库。我的意思是,即使我不使用线程,我是否需要调用CoInitialize(如我最初发布的代码)?我在编辑部分添加了一个非常好的演示,演示了如何使用WinHTTPRequest
接收事件。不,对于主线程,您不必这样做。作为证明,您可以检查的返回值,如果返回S_FALSE
,则COM库已经初始化。然后它返回;)谢谢你指出这一点。可能还有其他建议的替代方案吗?下载大文件的替代方案在WinInet.dll中:请参阅HttpOpenRequest()等。。
///////////////////////////////////////////////////////////////////////////////
///// 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.