向前进

——On My Way,From Office Boy

导航

wininet api 异步发大文件

http://support.microsoft.com/kb/242019

 

 

SAMPLE: SendRequestExAsync Uses HttpSendRequestEx Asynchronously

This article was previously published under Q242019

SUMMARY
Although you can use HttpSendRequestEx to post large amounts of data both synchr...

Although you can use HttpSendRequestEx to post large amounts of data both synchronously and asynchronously, using HttpSendRequestEx asynchronously can be a difficult task due to a number of issues involved in the timing and use of the various WinInet calls.

The sample SendRequestExAsync.cpp included in this article demonstrates how to properly call HttpSendRequestEx in an asynchronous WinInet application. You can compile and run SendRequestExAsync.cpp to send a large HTTP request to a server.

MORE INFORMATION
When you try to use HttpSendRequestEx and other associated APIs asynchronously,...

When you try to use HttpSendRequestEx and other associated APIs asynchronously, you need to be aware of the following issues:
  • All the normal issues that apply to standard asynchronous WinInet applications also apply when you use HttpSendRequestEx synchronously. For additional information on how to use WinInet APIs asynchronously, as well as sample code and a list of issues that are common to all asynchronous WinInet applications, click the article number below to view the article in the Microsoft Knowledge Base:
    275046  (http://support.microsoft.com/kb/275046/EN-US/ ) SAMPLE: Using AsyncHTTP to Call WinInet APIs Asynchronously
  • You will receive the completion for HttpSendRequestEx when all HTTP request headers have been successfully sent.

  • You will receive the completion for HttpEndRequest when all HTTP response headers are received from the server.

  • You cannot call InternetReadFile(Ex) until you receive a successful completion of HttpEndRequest. Therefore, you must wait until you get an INTERNET_STATUS_REQUEST_COMPLETE notification for HttpEndRequest before you can call InternetReadFile(Ex).

  • Even though your request handle has been opened with the asynchronous flag, InternetWriteFile tends to immediately return, indicating that the data has been sent successfully. This is because the operation that sends data across the underlying socket returns as soon as the data has entered the protocol's buffers. This is a very fast operation. Therefore, it completes before InternetWriteFile has a chance to return. However, if you repeatedly call InternetWriteFile too quickly or if you specify a particularly large buffer, you will eventually see it return ERROR_IO_PENDING. Only if you get an ERROR_IO_PENDING return will you receive an INTERNET_STATUS_REQUEST_COMPLETE notification for your call to InternetWriteFile.

The following file is available for download from the Microsoft Download Center:
SendRequestExAsync.exe (http://download.microsoft.com/download /ie5/sample/5/WIN98/EN-US/SendRequestExAsync.exe)
Release Date: Jan-24-2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591  (http://support.microsoft.com/kb/119591/EN-US/ ) How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. The SendRequestExAsync.exe file contains the following files:

Collapse this tableExpand this table
File name
SendRequestAsync.cpp
SendRequestAsync.dsp

REFERENCES
For additional information about using HttpSendRequestEx, click the article numb...

For additional information about using HttpSendRequestEx, click the article number below to view the article in the Microsoft Knowledge Base:
177188  (http://support.microsoft.com/kb/177188/EN-US/ ) FILE: Using HttpSendRequestEx for Large Post Requests

 

 

http://support.microsoft.com/kb/177188

 

SAMPLE: Using HttpSendRequestEx for Large POST Requests

This article was previously published under Q177188

On This Page

SUMMARY
This sample demonstrates proper usage of the HttpSendRequestEx function introduc...

This sample demonstrates proper usage of the HttpSendRequestEx function introduced in the Internet Explorer 4.0 WinInet.dll and documented in the Internet Client SDK.

The original HttpSendRequest function has a significant limitation: all the data for the request has to be provided in a single buffer when the function is called. This is often inconvenient, leads to poor performance in certain client applications, and may make it impossible to upload large amounts of data from client machines with limited memory. The new HttpSendRequestEx function allows a program to start a request, send out the data in small pieces as available, then end the request once all the data has been sent. Internet Explorer 4.0 must be installed on the computer in order for this function to work. The following file is available for download from the Microsoft Download Center:
Hsrex.exe (http://download.microsoft.com/download/ie4095/hsrex/1/win98/en-us/hsrex.exe)
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591  (http://support.microsoft.com/kb/119591/EN-US/ ) How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

MORE INFORMATION
Hsrex.exe is a self-extracting archive that contains BigPost.cpp (the code for t...

Hsrex.exe is a self-extracting archive that contains BigPost.cpp (the code for the demonstration program) and Readall.asp, an Active Server Pages (ASP) script that will read all data sent in a POST request. Readall.asp is provided as a sample target for BigPost, which can be used on Microsoft Internet Information Server (IIS) versions that support ASP. For other Web servers, you will need to provide a corresponding server script to read the data.

To compile the program with Microsoft Visual C++ 5.0, follow these steps:

  1. Run Visual C++ and create a new Win32 Console Application called "BigPost."
  2. In the directory where the project was created, run Hsrex.exe.
  3. Add BigPost.cpp to the BigPost project.
  4. Go to the Project Settings dialog box, click the Link tab, and add WinInet.lib to the "Object/library modules:" field.
  5. Ensure that Visual C++ is configured so that the compiler and linker will use the Wininet.h and Wininet.lib from the Internet Client SDK. If this is not done, a compiler or linker error will result. The include and library files included in Visual C++ do not contain the prototype and export of HttpSendRequestEx.
  6. Build the project. It will create BigPost.exe.
The program is run as follows:
BigPost <Size> <Server> <Path>
For example, the following would POST 1 megabyte (1024KB) to http://yourserver/scripts/ReadAll.asp:
BigPost 1024 yourserver /scripts/ReadAll.asp
The output from this would be as follows:
   Test of POSTing 1024KB with WinInet
   1048576 bytes sent.
   The following was returned by the server:
   1048576 bytes were read.

   Finished.
				

Notes

  • When using HttpSendRequestEx, the flag INTERNET_FLAG_NO_CACHE_WRITE should be used in the call to HttpOpenRequest, as shown in the following line from BigPost.cpp:
    HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", argv[3], NULL,
             NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
    					
  • The functionality demonstrated in this sample represents the full implementation of HttpSendRequestEx at this time. The other flags and parameters present in the documentation for this function are not yet implemented.
  • Even though the data can be sent in multiple buffers of whatever sizes are convenient to the programmer, the total number of bytes that will be sent in the request must be known before the request is begun, and the total number of bytes that are actually sent must match this number exactly, or an error will be returned by HttpEndRequest.

REFERENCES
For additional information, please see the following article in the Microsoft Kn...

For additional information, please see the following article in the Microsoft Knowledge Base:
177190  (http://support.microsoft.com/kb/177190/EN-US/ ) BUG: Error 12019 When Calling InternetWriteFile

 

posted on 2010-04-06 10:29  向前进  阅读(1812)  评论(0编辑  收藏  举报