Publish a form using WinHttp

Do I need to add headers before sending to the server?

For example, I'm currently trying to send a request along with the message data in 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 } 

if it works?

+4
source share
3 answers

I would suggest

  • you need to convey narrow lines not as wide as message data. I donโ€™t know if you indicate the type of content for the published data that will indicate the encoding - you probably should, if it's easy - either just re-encode the string as UTF-8, or just compile as a narrow string first
  • you may need an explicit end of line for post data, i.e. add \r\n to your (narrow) line - I donโ€™t know if the API will add it, since I assume you will make the same call for binary data.
+2
source

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); 
+5
source

According to this MSDN page , it looks like your sample code will work if you use the verb "POST" in WinHttpOpenRequest . If something doesnโ€™t work, run Fiddler in both the web browser and your application, and compare the headers created in both cases.

+1
source

Source: https://habr.com/ru/post/1333332/


All Articles