How to use HttpSendRequestEx with password-protected URLs (194700)



The information in this article applies to:

  • Microsoft Internet Client SDK 4.0
  • Microsoft Internet Client SDK 4.01
  • Microsoft Windows Internet Services (WinInet)

This article was previously published under Q194700

SUMMARY

You can use HttpSendRequestEx to send requests to a password-protected URL. This article outlines the different techniques you can use.

MORE INFORMATION

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.

REFERENCES

Internet Client SDK 4.0

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

184352 How to upload files to the Internet Information Server

177188 Using HttpSendRequestEx for large POST requests


Modification Type:MinorLast Reviewed:6/30/2006
Keywords:kbhowto KB194700