Discussion:
Filled Username/Password in InternetConnect(), but InternetReadFile() shows "unauthorized"
(too old to reply)
Jaw
2010-01-08 10:19:48 UTC
Permalink
Hi, All

I am wondering what's missed in my code, but have no luck after
surveying around.

Here is my code, it should be very simple and straight, hopefully
somebody could give me a hand....

[code]
HINTERNET hInternet = InternetOpen(L"TEST",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );

// "localhost" is a local HTTP server, it needs to be unauthorized to
connect
// I fills the username/password here
HINTERNET hConnection = InternetConnect(hInternet, L"localhost", 80,
L"admin", L"1234", INTERNET_SERVICE_HTTP, 0, 0 );


// index.cgi is a simple HelloWorld cgi program written by C
HINTERNET hData = HttpOpenRequest( hConnection , L"GET", L"/cgi-bin/
index.cgi", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );


// HTTP server show me "unauthorized" with HTMLed pMyBuffer
HttpSendRequest( hData, NULL, 0, NULL, 0);
DWORD dwRead = 0;
InternetReadFile( hData, pMyBuffer, 255, &dwRead );

[/code]


Am I missing something? I set up WireShark to inspect the HTTP
packets, and detected that the request packet doesn't carry with
authorization. Am I need to provide authentication by another way?



Any idea is appreciated, Jaw
benst
2010-02-03 17:34:02 UTC
Permalink
If you know the username and password in advance then try using

::InternetSetOption(hData ,
INTERNET_OPTION_USERNAME ,
(LPVOID)pszUsername, (DWORD)_tcslen(pszUsername))

::InternetSetOption(hData ,
INTERNET_OPTION_PASSWORD,
(LPVOID)pszPassword, (DWORD)_tcslen(pszPassword))

after your HttpOpenRequest()


If you don't know the username and password then try the InternetReadFile()
anyway (it might succeed if credentials are cached in-process), and if it
returns (requiring authentication) call InternetErrorDlg(). This will prompt
the user for credentials and store them in the request. Then retry the
request.

-- Ben.

Loading...