Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HTTPS Get request returns "Bad Request" Error

436 views
Skip to first unread message

Omer B.

unread,
Dec 6, 2004, 2:55:51 AM12/6/04
to
Hi,
I've written a c++ HTTP client that sends GET requests to download
pages. I use wininet and it works correctly. I want to support HTTPS
as well. I've changed the port of InternetConnect to
'INTERNET_DEFAULT_HTTPS_PORT' and added 'INTERNET_FLAG_SECURE' flag to
HttpOpenRequest.
I keep getting 'Bad Request' result when calling InternetReadFile on
HTTPS pages.

Any help???

Paul Baker [MVP, Windows - SDK]

unread,
Dec 6, 2004, 9:15:12 AM12/6/04
to
What happens if you attempt a similar request in Internet Explorer or your
browser?

What type of server is it?

Did you check the status code?

Paul

"Omer B." <om...@commtouch.com> wrote in message
news:135d32fd.04120...@posting.google.com...

omerb

unread,
Dec 6, 2004, 10:32:42 AM12/6/04
to
I wanted to access a page that is accessible by ssl only so I tried:
https://partnering.one.microsoft.com/Authenticate/login.aspx

I didnt check the status code, I checked the return value of each
WinInet API and all indicated success. However, the buffer that
received the data from InternetReadFile API contained "<h1>Bad
Request</h1>".

What am I missing?

Paul Baker [MVP, Windows - SDK]

unread,
Dec 6, 2004, 12:32:58 PM12/6/04
to
If the web server is giving you a status code indicating the reason for the
failure, WinInet will indicate success because it did successfully send the
request and is successfully able to get the response. You should check the
status code.

Paul

"omerb" <om...@commtouch.com> wrote in message
news:1102347162.5...@z14g2000cwz.googlegroups.com...

omerb

unread,
Dec 7, 2004, 3:04:14 AM12/7/04
to
Thanks for your replies.
After the call to HttpOpenRequest the status code is 0.
After the call to HttpSendRequest the status code is 400
(HTTP_STATUS_BAD_REQUEST)

Here is the code I use (removed the HttpQueryInfo calls):

CONST TCHAR *szAcceptType = "*/*";

HINTERNET hSession = InternetOpen("MyAgent",
INTERNET_OPEN_TYPE_PRECONFIG, "localhost", NULL, 0);

if(hSession == NULL)
{
cout << "error in InternetOpen: " << GetLastError() << endl;
return;
}

HINTERNET hConnect = InternetConnect(hSession, _T(sServerName.c_str()),
INTERNET_DEFAULT_HTTPS_PORT ,
NULL,
NULL, INTERNET_SERVICE_HTTP, 0, 1);

if(hConnect == NULL)
{
cout << "error in InternetConnect: " << GetLastError() << endl;
return;
}

HINTERNET hRequest = HttpOpenRequest(hConnect, "GET",
_T(strObject.c_str()),
HTTP_VERSION, "", &szAcceptType,
INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_NO_CACHE_WRITE, 1);

if(hRequest == NULL)
{
cout << "error in HttpOpenRequest" << endl;
return;
}

if(!HttpSendRequest(hRequest, NULL, 0, NULL, 0))
{
cout << "error in HttpSendRequest: " << GetLastError() << endl;
return;
}

DWORD dwSize;
if (!InternetQueryDataAvailable(hRequest,&dwSize,0,0))
{
cout << "Error Occured in calling InternetQueryDataAvailable"
<< endl;
return;
}

DWORD dwDownloaded;
char szBuff[1024] = {0};
if(InternetReadFile(hRequest,(LPVOID)szBuff,1023,&dwDownloaded))
{
while(dwDownloaded > 0)
{
cout << szBuff;

if(!InternetReadFile(hRequest,(LPVOID)szBuff,1023,&dwDownloaded))
{
cout << "error" << endl;
return;
}
}
}

omerb

unread,
Dec 7, 2004, 3:03:36 AM12/7/04
to

Stephen Sulzer

unread,
Dec 7, 2004, 5:44:43 AM12/7/04
to

This may not be related to the cause of the problem, but there is a bug in
your code. The szAcceptType array variable is not declared correctly. For
the szAcceptType parameter, HttpOpenRequest expects an array of strings
terminated by a NULL array element. This is not what your code is doing. The
result of this bug is that you may be passing malformed additional request
headers to the server. Unfortunately, HttpOpenRequest does not seem to
validate this parameter.

So your szAcceptType variable declaration should look like this instead:

const TCHAR * szAcceptType[] = { _T("*/*"), NULL };


And change your call to HttpOpenRequest to pass szAcceptType accordingly (no
need for the extra dereference):

HINTERNET hRequest = HttpOpenRequest(hConnect, "GET",
_T(strObject.c_str()),

HTTP_VERSION, "", szAcceptType,


INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_NO_CACHE_WRITE, 1);


Hope that helps.

Stephen


omerb

unread,
Dec 7, 2004, 6:38:13 AM12/7/04
to
Wow - it DID solve the problem.
Thanks a lot !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0 new messages