5

Do i need to add any headers before making a post to server?

For example, Currently I'm trying to send a request along with the post data this way,

  LPCWSTR post = L"name=User&subject=Hi&message=Hi";

    if (!(WinHttpSendRequest( hRequest, 
                            WINHTTP_NO_ADDITIONAL_HEADERS,
                            0, (LPVOID)post, wcslen(post), 
                            wcslen(post), 0)))
    {
          //error
    }

should this work?

user963241
  • 6,758
  • 19
  • 65
  • 93

3 Answers3

6

What worked for me:

    LPSTR  post = "log=test";//in my php file: if(isset($_POST['log']))
    hRequest = WinHttpOpenRequest(hConnect,
                                    L"POST",
                                    L"/test.php",
                                    NULL,
                                    WINHTTP_NO_REFERER,
                                    WINHTTP_DEFAULT_ACCEPT_TYPES,
                                    0);
    bResults = WinHttpSendRequest(hRequest,
                                    L"content-type:application/x-www-form-urlencoded",
                                    -1,
                                    post,
                                    strlen(post),
                                    strlen(post),
                                    NULL);
OhadM
  • 4,687
  • 1
  • 47
  • 57
3

I'd guess

  • you need to pass narrow strings not wide as the post data. I don't know if you're specifying a content type for the posted data which would specify the encoding - you probably should if it's easy to - or just re-encode the string as UTF-8, or just assemble as a narrow string in the first place
  • you might need an explicit end-of-line on the post data, i.e. add \r\n to your (narrow) string - I don't know if the API's going to add one since I assume you'd make the same call for binary data.
Rup
  • 33,765
  • 9
  • 83
  • 112
  • I used `LPCSTR` for post and added `\r\n` but it still returns the form url source code itself rather than posting it. It has worked for me with libcurl before. – user963241 Jan 22 '11 at 19:03
  • For setting cookies, I'm using `WinHttpAddRequestHeaders` which accepts `LPCWSTR` for header string. For example: `"Cookie: session=key"` – user963241 Jan 22 '11 at 19:10
  • Done! 1.) I needed to specify the site with `www.` 2.) I needed to use narrow string. 3.) `\r\n` wasn't necessary. – user963241 Jan 23 '11 at 23:31
  • However, post data isn't accepting a semicolon ';' in message string. instead it sends the message part before semicolon and ignores the latter part of message after ';'. is there anyway to send ';'? – user963241 Jan 24 '11 at 00:06
  • 1
    The post data is sent as an URL-encoded string so you'll need to write `;` as `%3B`. Characters other than letters, numbers and a handful of others should be written as % then a two-digit hex value. This is specified in [RFC1738](http://tools.ietf.org/html/rfc1738#section-5) but it isn't very readable. You should be able to find existing URL-encoding functions in C++ you can use here, or there here's someone's [test encode / decode page](http://meyerweb.com/eric/tools/dencoder/) you can try. Sorry about the \r\n red-herring! – Rup Jan 24 '11 at 10:57
1

According to this MSDN page, it sure looks like your code sample will work, assuming you have are using the "POST" verb in WinHttpOpenRequest. If things just aren't working, then run Fiddler on both a web browser and your app, and compare headers generated from both cases.

Chris O
  • 5,017
  • 3
  • 35
  • 42
  • 1
    Well no - the lpOptional parameter is a buffer, not a wide string, so he's transmitting the string as unicode data which the server probably isn't expecting. And he's passing the string character length where the API expects the byte length so he's only transmitting half the string in any case. – Rup Jan 21 '11 at 15:06
  • @Rup Yes, you are correct, the wcslen will give the char count, not the buffer size. A half a string is a lot like a half a bee... – Chris O Jan 22 '11 at 01:30