Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting form using WinHttp

Tags:

c++

winhttp

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?

like image 430
user963241 Avatar asked Jan 20 '23 20:01

user963241


2 Answers

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);
like image 188
OhadM Avatar answered Jan 31 '23 07:01

OhadM


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.
like image 24
Rup Avatar answered Jan 31 '23 08:01

Rup