Discussion:
HTTPS Get request returns "Bad Request" Error
(too old to reply)
Omer B.
2004-12-06 07:55:51 UTC
Permalink
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]
2004-12-06 14:15:12 UTC
Permalink
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
Post by Omer B.
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???
omerb
2004-12-06 15:32:42 UTC
Permalink
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]
2004-12-06 17:32:58 UTC
Permalink
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
Post by omerb
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?
omerb
2004-12-07 08:03:36 UTC
Permalink
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
2004-12-07 08:04:14 UTC
Permalink
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;
}
}
}
Stephen Sulzer
2004-12-07 10:44:43 UTC
Permalink
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
2004-12-07 11:38:13 UTC
Permalink
Wow - it DID solve the problem.
Thanks a lot !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Continue reading on narkive:
Loading...