views:

104

answers:

1

Trying to log the application version details in our weblogs using the headers:

Should be a one liner..but for some reason whenever I specify anything but NULL for the headers it causes an exception (InternetOpenUrl() call returns NULL) - anyone able to explain why?

  CInternetSession internet;
  CHttpFile * pHttpFile;
  CString headers;// = APPLICATION_SUITE_NAME;
  DWORD dwHeadersLength = -1;

  headers.Format("%s %s %s\n",APPLICATION_SUITE_NAME,SHORT_APPLICATION_VERSION,BUILDNO_STR);

  pHttpFile =(CHttpFile *) internet.OpenURL(lpszURL, 1, INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_DONT_CACHE, headers, dwHeadersLength);

Without the headers, dwHeadersLength parameter (eg. pass in NULL,-1) then it goes through fine and I see the request come through to our website. But why does it fail if I pass in custom headers?

A: 

Does your CString resolve to CStringA or CStringW? If the latter (i.e. wide-char), here's a bit from MSDN (http://msdn.microsoft.com/en-us/library/aa384247%28VS.85%29.aspx):

If dwHeadersLength is -1L and lpszHeaders is not NULL, the following will happen: If HttpSendRequestA is called, the function assumes that lpszHeaders is zero-terminated (ASCIIZ), and the length is calculated. If HttpSendRequestW is called, the function fails with ERROR_INVALID_PARAMETER.

I'm mentioning HttpSendRequest() because, in fact, CInternetSession::OpenURL() calls InternetOpenUrl(), whose documentation for the lpszHeaders parameters sends you to the doc page for HttpSendRequest().

EDIT: Another possibility is that the call fails because the headers do not seem to be in canonical format: according to the HTTP 1.1 spec, they ought to be in "name: value" format, and each header should be separated from the next one by CRLF.

Guido Domenici
Hmm.. we use MBCS here, so I would presume CStringW - but that would mean all I'd need is a \0 at the end of my string OR I manually pass in the length of the string. I've tried both unfortunately and it still returns NULL at that point - what's annoying is that I can't trace in any further and find out why (so I can't go inside InternetOpenUrl()).
Jesse
Edited my answer with another possible error cause.
Guido Domenici
Canonical format was the key! - Added "User-Agent: " to the start of my Format and it worked perfectly! Thank you for your help!
Jesse