CInternetSession::OpenURL exception if headers are defined. Works fine if not. Why?
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_TRA开发者_StackOverflow中文版NSFER_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?
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.
精彩评论