Discussion:
HttpQueryInfo returning wrong status code 200 not 301
(too old to reply)
Brian Cryer
2005-12-23 10:01:33 UTC
Permalink
I am using HttpQueryInfo to return the HTTP Status Code from a server. I had
thought this was all working fine, but I've come across a case (www.url.com)
where the server returns 301 (Moved Permanently), but HttpQueryInfo returns
200 (OK). Is there a flag that I should be using with HttpQueryInfo that I'm
missing?

The code I'm using (Delphi - but it should be easy enough to follow for
non-Delphi users) is:

hInet := InternetOpen(PChar(application.title),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,nil,nil,0);
hConnect := InternetOpenUrl(hInet,PChar(url),nil,0,INTERNET_FLAG_NO_UI,0);
if not Assigned(hConnect) then
result := false
else
begin
// Create a request for the url.
dummy := 0;
bufLen := Length(infoBuffer);
okay := HttpQueryInfo(hConnect,HTTP_QUERY_STATUS_CODE,@infoBuffer[0],
bufLen,dummy);
if not okay then
// Probably working offline, or no internet connection.
result := False
else
begin
reply := infoBuffer;
if reply = '200' then // File exists, all ok.
result := True
else if reply = '301' then // Moved permanently.
result := False
else if reply = '401' then // Not authorised. Assume page exists,
but we can't check it.
result := True
else if reply = '404' then // No such file.
result := False
else if reply = '500' then // Internal server error.
result := False
else
// Shouldn't get here! It means there is a status code left
unhandled.
result := False;
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInet);

I know I could ditch WinInet and open the port directly, but I'd rather not.
Any other ideas would be apprecaited.

Thanks.
--
Brian Cryer
www.cryer.co.uk/brian
Scherbina Vladimir
2005-12-23 20:42:41 UTC
Permalink
Hello Brian.

HttpQueryInfo returns correct error code, because when you make a post to
www.url.com it returnes "Location" header and InternetOpenUrl automatically
redirects you on that location. In our case it's :

Location:
http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=URL.COM

When you call HttpQueryInfo it returns you response from
http://apps5.oingo.com domain.
To avoid such situation you need manually open request and get's it's error
code.
Here is my approach to your problem (C++ source, sorry I don't have Delphi
over here)

/// Note, I did not make error checks, this code is just demonstration

#include <windows.h>
#include <wininet.h>

#pragma comment(lib, "wininet.lib")

void main (void)
{
HINTERNET hInternet = ::InternetOpen( ( "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1)" ), INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0 );

HINTERNET hConnection = ::InternetConnect(hInternet, "www.url.com", 80,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL );

HINTERNET hRequest = ::HttpOpenRequest(hConnection, ( "POST" ), "/",
( "HTTP/1.1" ), NULL, NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE, NULL );

if ( HttpSendRequest( hRequest, NULL, 0, NULL, 0 ) == TRUE)
{

DWORD dwIndex = 0;
DWORD dwErr = 0;
char szBuff[MAX_PATH] = {0};
DWORD dwLeng = sizeof(szBuff);

if (HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &szBuff, &dwLeng,
&dwIndex) == TRUE)
{ /// I get here 301 in szBuff !
/// do processing...
MessageBox (NULL, szBuff, szBuff, MB_OK);
}
}

InternetCloseHandle (hInternet);
InternetCloseHandle (hRequest);
InternetCloseHandle (hConnection);
}
--
Vladimir
Post by Brian Cryer
I am using HttpQueryInfo to return the HTTP Status Code from a server. I had
thought this was all working fine, but I've come across a case
(www.url.com)
where the server returns 301 (Moved Permanently), but HttpQueryInfo returns
200 (OK). Is there a flag that I should be using with HttpQueryInfo that I'm
missing?
The code I'm using (Delphi - but it should be easy enough to follow for
hInet := InternetOpen(PChar(application.title),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,nil,nil,0);
hConnect :=
InternetOpenUrl(hInet,PChar(url),nil,0,INTERNET_FLAG_NO_UI,0);
if not Assigned(hConnect) then
result := false
else
begin
// Create a request for the url.
dummy := 0;
bufLen := Length(infoBuffer);
bufLen,dummy);
if not okay then
// Probably working offline, or no internet connection.
result := False
else
begin
reply := infoBuffer;
if reply = '200' then // File exists, all ok.
result := True
else if reply = '301' then // Moved permanently.
result := False
else if reply = '401' then // Not authorised. Assume page exists,
but we can't check it.
result := True
else if reply = '404' then // No such file.
result := False
else if reply = '500' then // Internal server error.
result := False
else
// Shouldn't get here! It means there is a status code left
unhandled.
result := False;
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInet);
I know I could ditch WinInet and open the port directly, but I'd rather not.
Any other ideas would be apprecaited.
Thanks.
--
Brian Cryer
www.cryer.co.uk/brian
Brian Cryer
2005-12-23 23:32:04 UTC
Permalink
That makes sense. I'll give your code a go (C/C++ is fine).

Thanks.
Post by Scherbina Vladimir
Hello Brian.
HttpQueryInfo returns correct error code, because when you make a post to
www.url.com it returnes "Location" header and InternetOpenUrl
automatically
http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=URL.COM
Post by Scherbina Vladimir
When you call HttpQueryInfo it returns you response from
http://apps5.oingo.com domain.
To avoid such situation you need manually open request and get's it's error
code.
Here is my approach to your problem (C++ source, sorry I don't have Delphi
over here)
/// Note, I did not make error checks, this code is just demonstration
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
void main (void)
{
HINTERNET hInternet = ::InternetOpen( ( "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1)" ), INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0 );
HINTERNET hConnection = ::InternetConnect(hInternet, "www.url.com", 80,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL );
HINTERNET hRequest = ::HttpOpenRequest(hConnection, ( "POST" ), "/",
( "HTTP/1.1" ), NULL, NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE, NULL );
if ( HttpSendRequest( hRequest, NULL, 0, NULL, 0 ) == TRUE)
{
DWORD dwIndex = 0;
DWORD dwErr = 0;
char szBuff[MAX_PATH] = {0};
DWORD dwLeng = sizeof(szBuff);
if (HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &szBuff, &dwLeng,
&dwIndex) == TRUE)
{ /// I get here 301 in szBuff !
/// do processing...
MessageBox (NULL, szBuff, szBuff, MB_OK);
}
}
InternetCloseHandle (hInternet);
InternetCloseHandle (hRequest);
InternetCloseHandle (hConnection);
}
--
Vladimir
Post by Brian Cryer
I am using HttpQueryInfo to return the HTTP Status Code from a server. I had
thought this was all working fine, but I've come across a case (www.url.com)
where the server returns 301 (Moved Permanently), but HttpQueryInfo returns
200 (OK). Is there a flag that I should be using with HttpQueryInfo that I'm
missing?
The code I'm using (Delphi - but it should be easy enough to follow for
hInet := InternetOpen(PChar(application.title),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,nil,nil,0);
hConnect :=
InternetOpenUrl(hInet,PChar(url),nil,0,INTERNET_FLAG_NO_UI,0);
if not Assigned(hConnect) then
result := false
else
begin
// Create a request for the url.
dummy := 0;
bufLen := Length(infoBuffer);
bufLen,dummy);
if not okay then
// Probably working offline, or no internet connection.
result := False
else
begin
reply := infoBuffer;
if reply = '200' then // File exists, all ok.
result := True
else if reply = '301' then // Moved permanently.
result := False
else if reply = '401' then // Not authorised. Assume page exists,
but we can't check it.
result := True
else if reply = '404' then // No such file.
result := False
else if reply = '500' then // Internal server error.
result := False
else
// Shouldn't get here! It means there is a status code left
unhandled.
result := False;
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInet);
I know I could ditch WinInet and open the port directly, but I'd rather not.
Any other ideas would be apprecaited.
Thanks.
--
Brian Cryer
www.cryer.co.uk/brian
Loading...