I am having a problem with detecting 404's in Wininet, specifically using the HttpQueryInfo function. I have asked on 2 separate forums (SO and Daniweb), and no one has been able to answer. (SO had an answer, but it did not fix the problem).
What I am trying to do is check if a page exists on the web server by querying for a 404 status. Code:
First, line 3 is non-standard C++. I am pretty sure most compilers will reject line #3 in your code snippet. Which compiler are you using?
In any case, the standard way would be to allocate memory using a memory-allocator function, namely the new operator or malloc or a Windows-specific function like LocalAlloc().
Second, if you read the Remarks section @ MSDN Online about the HttpQueryInfo() function (http://msdn.microsoft.com/en-us/library/aa384238(VS.85).aspx), you'll see that the data type returned by a query for the status code is a number, not a string, so your use of atoi() is 100% incorrect as per the documentation. All you have to do is reinterpret the buffer as a DWORD. You can confirm you have a DWORD if after the function call, the value of statCharLen == sizeof(DWORD).
I have made some modifications to my code, but the buffer length is not the size of a DWORD :/
It dosent really make sense to me, it should return buffer the size of a DWORD because I am requesting only the status code.
You are missing the HTTP_QUERY_FLAG_NUMBER flag as per the Remarks section @ MSDN Online and you should have reinterpreted *pStatCode, not &pStatCode. Your buffer is also not needed. You can do it like this:
Thanks, the statCodeLen is now the correct size (4 bytes), but there is no data in the statCode variable. When printed out, it is still "0". I'm not sure what to make of this, the code I am using is yours exactly.
Sorry I cant figure this out myself further, but it doesn't really make much sense.
If you are using my exact code, you'll notice that the DWORD statCode is not initialized to zero before calling the function. If you are debugging, most likely it contains a value other than zero before the function call. If after the function call you are getting zero, it means this is the value being written by HttpQueryInfo(). More I cannot tell you. I have never used this function before, so I have no prior experience.
If the function is indeed not failing, this is the code you are getting. My recommendation: Make an HTTP request that you know for sure it either works (HTTP_STATUS_OK) or doesn't (verify it using an Internet browser). Then compare to what your program is telling you. I would start here.