【问题标题】:POST Request in WinHttp c++WinHttp c ++中的POST请求
【发布时间】:2016-07-30 10:10:44
【问题描述】:

我正在尝试使用 WinHTTP api Click to the Microsoft Guide 在 c++ 中发出 POST 请求,问题是微软网页中可用的示例是“GET”请求,所以我想出了这个代码搜索互联网:

首先我们调用代码:

HttpsWebRequestPost("example.com", "/api.php?action=UserLogin", "loginUsername=" + USERNAME + "&loginPassword=" + PASSWORD + "&url=/index.php?page=Portal");

然后:

#include <Windows.h>
#include <WinHttp.h>
#include <stdio.h>
#include <iostream> //getchar
#include <fstream>

#pragma comment(lib, "winhttp.lib")

using namespace std;

std::wstring get_utf16(const std::string &str, int codepage)
{
    if (str.empty()) return std::wstring();
    int sz = MultiByteToWideChar(codepage, 0, &str[0], (int)str.size(), 0, 0);
    std::wstring res(sz, 0);
    MultiByteToWideChar(codepage, 0, &str[0], (int)str.size(), &res[0], sz);
    return res;
}

string HttpsWebRequestPost(string domain, string url, string dat)
{
    //Extra
    LPSTR  data = const_cast<char *>(dat.c_str());;
    DWORD data_len = strlen(data);


    wstring sdomain = get_utf16(domain, CP_UTF8);
    wstring surl = get_utf16(url, CP_UTF8);
    string response;

    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect(hSession, sdomain.c_str(),
            INTERNET_DEFAULT_HTTP_PORT, 0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest(hConnect, L"POST", surl.c_str(),
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            0);

    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS, 0,
            (LPVOID)data, data_len,
            data_len, 0);


    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest, NULL);

    // Keep checking for data until there is nothing left.
    if (bResults)
    {
        do
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n",
                    GetLastError());

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize + 1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                dwSize = 0;
            }
            else
            {
                // Read the data.
                ZeroMemory(pszOutBuffer, dwSize + 1);

                if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
                    dwSize, &dwDownloaded))
                    printf("Error %u in WinHttpReadData.\n", GetLastError());
                else
                    //printf("%s", pszOutBuffer);
                    response = response + string(pszOutBuffer);
                // Free the memory allocated to the buffer.
                delete[] pszOutBuffer;
            }
        } while (dwSize > 0);
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

    return response;

}

但使用 WireShark 我只能得到:

Hypertext Transfer Protocol
    POST ***************** HTTP/1.1\r\n
    Connection: Keep-Alive\r\n
    User-Agent: WinHTTP Example/1.0\r\n
    Content-Length: **\r\n
    Host: ******\r\n
    \r\n

任何人都可以帮助解决此问题或知道更简单的方法吗? 谢谢

【问题讨论】:

    标签: c++ http c++11 post winhttp


    【解决方案1】:

    为了让 PHP(或任何其他后处理语言)识别 POST 数据,添加以下内容:

    LPCWSTR additionalHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
    DWORD headersLength = -1;
    
    bResults = WinHttpSendRequest(  hRequest,
                                    additionalHeaders,
                                    headersLength ,
                                    (LPVOID)data,
                                    data_len,
                                    data_len, 
                                    0);
    

    代码的其余部分是正常的,应该可以工作:

    【讨论】:

      【解决方案2】:

      当我尝试在键(信息)和值(这是一些信息)对中发布文本时,以下代码适用于我。最后返回状态码200。

      int main()
      {
          DWORD dwSize = 0;
          LPVOID lpOutBuffer = NULL;
          BOOL  bResults = FALSE;
          HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;
      
          // Use WinHttpOpen to obtain a session handle.
          hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0",
              WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
              WINHTTP_NO_PROXY_NAME,
              WINHTTP_NO_PROXY_BYPASS, 0);
      
          std::wstring url{ L"http://app.pomdit.com:3010/uploadfile" };
          URL_COMPONENTS components{};
          components.dwStructSize = sizeof(components);
          components.dwHostNameLength = (DWORD)-1;
          components.dwUrlPathLength = (DWORD)-1;
          if (!WinHttpCrackUrl(url.c_str(), static_cast<DWORD>(url.length()), 0, &components)) {
              wprintf((L"WinHttpCrackUrl(): " + ErrorMessage(GetLastError())).c_str());
          }
      
          std::wstring hostName(components.lpszHostName ? std::wstring{ components.lpszHostName, components.dwHostNameLength } : L"localhost");
          // Specify an HTTP server.
          if (hSession)
              hConnect = WinHttpConnect(hSession, hostName.c_str(),
                                        components.nPort, 0);
      
          // Create an HTTP request handle.
          if (hConnect)
              hRequest = WinHttpOpenRequest(hConnect, L"POST", components.lpszUrlPath,
                  NULL, WINHTTP_NO_REFERER,
                  WINHTTP_DEFAULT_ACCEPT_TYPES,
                  0);
      
          const WCHAR* ContentType =
              L"Content-Type: multipart/form-data;boundary = 19024605111143684786787635207";
      
              const char* MultipartRequestBody =
              "--19024605111143684786787635207\r\n"
              "Content-Disposition: form-data; name=\"info\"\r\n"
              "\r\n"
              "this is some info\r\n"
              "--19024605111143684786787635207\r\n";
      
              bResults = WinHttpSendRequest(hRequest, ContentType, wcslen(ContentType),
                  (LPVOID)MultipartRequestBody,
                  strlen(MultipartRequestBody),
                  strlen(MultipartRequestBody),
                  NULL);
      
          // End the request.
          if (bResults)
              bResults = WinHttpReceiveResponse(hRequest, NULL);
      
          // First, use WinHttpQueryHeaders to obtain the size of the buffer.
          if (bResults)
          {
              DWORD status{}, len = sizeof(status);
              bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &len, NULL);
      
              if (bResults)
              {
                  printf("Status code = %d.\n", status);
              }
          }
          else
          {
              wprintf(L"WinHttpReceiveResponse(): %s\n", ErrorMessage(GetLastError()).c_str());
          }
      
          // Close any open handles.
          if (hRequest) WinHttpCloseHandle(hRequest);
          if (hConnect) WinHttpCloseHandle(hConnect);
          if (hSession) WinHttpCloseHandle(hSession);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-31
        • 2014-11-05
        • 2014-05-24
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 2019-01-17
        • 2017-05-22
        相关资源
        最近更新 更多