Newsgroups : Microsoft : microsoft.public.inetsdk.programming.wininet : 2005 Dec : HttpQueryInfo returning wrong status code 200 not 301

www.cryer.info
Managed Newsgroup Archive

HttpQueryInfo returning wrong status code 200 not 301

Subject:HttpQueryInfo returning wrong status code 200 not 301
Posted by:"Brian Cryer" (brian.cry..@127.0.0.1.ntlworld.com)
Date:Fri, 23 Dec 2005 10:01:33 GMT

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

Replies:

www.cryer.info
Managed Newsgroup Archive