HOWTO: How to Handle Proxy Authorization with WinInetID: Q195650
|
WinInet applications attempting to access files through a proxy that requires a login will fail unless the proxy is provided with a valid username and password. This article will explain the different options available to handle this situation.
For the sake of clarity, error handling has been removed from most of the
code in this article. If you use any of this code in your own program,
please implement error handling appropriately. As well, anywhere you find
"...", code has been removed. Please reference the HttpDump and Tear
samples for complete implementations of WinInet coding.
The following code snippet was taken from the HttpDump sample. It
illustrates how to capture a proxy authorization request
(HTTP_STATUS_PROXY_AUTH_REQ):
HttpSendRequest (hReq, NULL, 0, NULL, 0);
HttpQueryInfo (hReq, HTTP_QUERY_STATUS_CODE |
HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL);
if (dwCode == HTTP_STATUS_PROXY_AUTH_REQ)
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_PROXY_AUTH_REQ)
if ( InternetErrorDlg (GetDesktopWindow(),
hReq, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
NULL) == ERROR_INTERNET_FORCE_RETRY)
goto again;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
Q189094 Calling CHttpFile::ErrorDlg Function Causes Errors 127 & 2The drawback of the using InternetErrorDlg is that the function call displays a user interface. In some cases, this is not desirable.
if (dwCode == HTTP_STATUS_PROXY_AUTH_REQ)
{
// read the data off the request handle and setup buffer see
// handler HttpDump handler for HTTP_STATUS_DENIED for details
...
InternetSetOption (hConnect, INTERNET_OPTION_PROXY_USERNAME,
(LPVOID) szUser, lstrlen (szUser);
InternetSetOption (hConnect, INTERNET_OPTION_PROXY_PASSWORD,
(LPVOID) szPass, lstrlen (szPass);
// calls HttpSendRequest again - see HttpDump
goto again;
char* rangeHeader = "Proxy-Authorization: Basic wWdkd2284lssdj\r\n";
DWORD dwBuffSize = strlen(rangeHeader);
if (dwCode == HTTP_STATUS_PROXY_AUTH_REQ)
{
HttpAddRequestHeaders(hReq, rangeHeader, dwBuffSize,
HTTP_ADDREQ_FLAG_ADD );
// calls HttpSendRequest again - see HttpDump
goto again;
Q191239 SAMPLE: Sample Base 64 Encoding and DecodingThe format of the username and password that needs to be encrypted is "username:password" including the colon. Please reference RFC2068 - "Hypertext Transfer Protocol -- HTTP/1.1" for further details on this.
RFC 2068 / Hypertext Transfer Protocol -- HTTP/1.1
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by
Robert Duke, Microsoft Corporation
Additional query words:
Keywords : kbnokeyword kbIE400
Version : WINDOWS:3.0,4.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 23, 1999