Posting form using WinHttp

不想你离开。 提交于 2019-12-06 05:23:21

问题


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?


回答1:


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.



回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/4530088/posting-form-using-winhttp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!