浙林龙哥

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://support.microsoft.com/default.aspx?scid=kb;en-us;194700

This article was previously published under Q194700

This is the usual sequence of APIs used with HttpSendRequest:
   InternetConnect ()
   HttpOpenRequest ()
   HttpSendRequestEx ()
   HttpEndRequest ()
				

Method 1

If the user name and password are known before sending the request (that is, they don't have to be dynamically entered by the user), then user name and password can be supplied directly to the InternetConnect API. However, unlike HttpSendRequest, HttpSendRequestEx will not resubmit a request on its own after receiving the "401 Access Denied" status code from the server. Therefore, HttpEndRequest will fail with an ERROR_INTERNET_FORCE_RETRY error. This error message from HttpEndRequest indicates that the application must go back to HttpSendRequestEx and send all the buffers with InternetWriteFile again.

Method 2

If it is not possible to supply credentials in the InternetConnect API, then you must use the following steps:
  1. Similarly to HttpSendRequest, the status code of the request may be determined by calling HttpQueryInfo (hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG). With HttpSendRequestEx, HttpQueryInfo must be called after HttpEndRequest, not after HttpSendRequestEx.
  2. Valid credentials can be entered either with InternetErrorDlg() or by calling InternetSetOption with INTERNET_OPTION_USERNAME and INTERNET_OPTION_PASSWORD options.
  3. Similarly to method 1, the application should go back toHttpSendRequestEx.

Both of the methods above have a serious drawback: Because HttpSendRequestEx is used to send large amounts of data, resubmitting the entire data upon receiving the ERROR_INTERNET_FORCE_RETRY error or the 401 status code may waste network bandwidth and time. Method 3 is the preferred method of handling user authentication with HttpSendRequestEx:

Method 3

This method involves sending an auxiliary request for the URL via HttpSendRequest. Note that HttpSendRequestEx should be called on the same handle as HttpSendRequest. This will ensure that the request sent by HttpSendRequestEx will be sent over the connection authenticated by the first call to HttpSendRequest. Reusing the connection (using "Keep-Alive" connection) is necessary for NTLM (NT LAN manager authentication) support. To preserve bandwidth and time, neither request nor reply should have large amounts of data. The best way to accomplish this is to send the same type of request with HttpSendRequest as HttpSendRequestEx, but with the 0 content length.

The following steps show how to use an auxiliary request. It assumes that large amounts of data need to be POSTed to /Scripts/Poster.exe URL:
 hOpen = InternetOpen (...)
 hConnect = InternetConnect (hOpen, ...)

 // Note INTERNET_FLAG_KEEP_CONNECTION flag needed for NTLM

 hRequest = HttpOpenRequest (hConnect, "POST", 
                            "/scripts/poster.exe", 
                            lpszVersion, lpszReferer, lpszAcceptTypes, 
                            INTERNET_FLAG_KEEP_CONNECTION, dwContext)
 HttpSendRequest (hRequest, NULL, 0, NULL, 0);

 // at this point normal authentication logic can be used. If
 // credentials are supplied in InternetConnect, then Wininet will
 // resubmit credentials itself.  See HttpDump Internet Client SDK sample
 // for more information. 

 // Read all returned data with InternetReadFile () 
 do
 {
     InternetReadFile (hRequest, ..., &dwSize);
 }
 while ( dwRead != 0);



 // Now send real request that will be send with HttpSendRequestEx. By
 // this time all authentication is done

 // Note that we are using the same handle as HttpSendRequest<BR/>
 Again:
 HttpSendRequestEx (hRequest, ...);
 do
 {
    InternetWriteFile()
                  }
 while () ; // stop condition
 if ( !HttpEndRequest ())
 {
    if ( ERROR_INTERNET_FORCE_RETRY == (dwError= GetLastError() ) )
    {
         Goto again;
    }
    // handle other errors here
 }
				
Performing all the authentication in HEAD request causes WinInet to create an appropriate authorization header that is sent with a large request submitted by HttpSendRequestEx.
 
====================================
结论:
解决方案一:
1.HttpOpenRequest 里面要 INTERNET_FLAG_KEEP_CONNECTION,http版本:HTTP/1.1
 
2.要把windows集成认证的用户名、密码,填入到InternetConnectr的第四、五个参数中去

3.
先用HttpSendRequest 试探性地访问资源,并用
InternetReadFile 读完返回结果
要把这次的hRequest 关掉,再打开一个hRequest(否则上传时Content-Lenth为0),或者在第四步自己把Content-Length设置好

4.使用HttpSendRequestEx -InternetWriteFile-HttpEndRequest 组合上传文件,如果最后返回ERROR_INTERNET_FORCE_RETRY则这步重新来过。
解决方案二(测试通过):
1.HttpOpenRequest 里面要 INTERNET_FLAG_KEEP_CONNECTION,http版本:HTTP/1.1
2.使用HttpSendRequestEx -InternetWriteFile-HttpEndRequest 组合上传文件,如果最后返回ERROR_INTERNET_FORCE_RETRY则这步重新来过。
 
posted on 2009-03-04 23:46  浙林龙哥  阅读(1680)  评论(1编辑  收藏  举报