WinInet InternetOpenUrl() request headers RRS feed

  • Question

  • I'm trying to create a native C++ application that reads information from the internet. I'm using InternetOpen() followed by InternetOpenUrl(). All work perfeclty well, but... if the lpszHeaders parameter of InternetOpenUrl() is not L"", then some servers return a 400 bad request error. I also don't know how I should specify multiple headers.

    I need to send an Accept and Accept-Encoding header. Anyone have a clue how to specify the headers parameter? It's driving me crazy.

    I'm running Win XP Pro SP3 IE7.
    Thursday, May 21, 2009 10:43 PM


  • I finally found the problem. Unfortunately, I was unable to post the text below to the community content of InternetOpenUrl().

    Do not use InternetOpenUrlW() with lpszHeaders set

    You should not use the Unicode version of this function if you want to send additional headers.

    If dwHeadersLength excludes the terminating null, the function crashes with error ERROR_HTTP_HEADER_NOT_FOUND.

    If it does include the terminating null character, this null character is also sent to the server. This works on Apache servers, but various servers respond with HTTP 400 Bad Request errors.

    You can solve the problem by calling WideCharToMultiByte() to convert the URL to ANSI and then calling InternetOpenUrlA(), which accepts dwHeadersLength not including the terminating null.
    • Marked as answer by Blijbol Friday, May 22, 2009 11:39 AM
    Friday, May 22, 2009 11:39 AM