任何人都知道为啥这个带有 WinHttp 的 POST 失败了
Posted
技术标签:
【中文标题】任何人都知道为啥这个带有 WinHttp 的 POST 失败了【英文标题】:Anyone know why this POST with WinHttp is failing任何人都知道为什么这个带有 WinHttp 的 POST 失败了 【发布时间】:2013-08-25 21:58:34 【问题描述】:我正在尝试向 php 脚本发送 POST 请求。我有 GET 工作,但 POST 有一些问题。当它应该显示 post 参数的值时,结果总是空白。
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "wininet.lib")
void PrintError(char *szFunc)
char szErr[128];
wsprintf(szErr, "%s: %d\n", szFunc, GetLastError());
OutputDebugString(szErr);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
BOOL bCookie;
DWORD dwFlags;
DWORD dwReadSize;
DWORD dwBuffLen = sizeof(dwFlags);
HINTERNET hInternet, hSession, hRequest;
char szBuffer[256 * 1024] = "";
SIZE_T nBufferSize = 0;
hInternet = InternetOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hInternet == NULL)
PrintError("InternetOpen");
return 0;
hSession = InternetConnect(hInternet, "localhost", 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
if(hSession == NULL)
PrintError("InternetConnet");
return 0;
hRequest = HttpOpenRequest(hSession, "GET", "/index.php", "HTTP/1.1", NULL, NULL, INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES, NULL);
if(hRequest == NULL)
PrintError("HttpOpenRequest");
return 0;
// Need to do this to support self signed SSL certificates
InternetQueryOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
dwFlags = INTERNET_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_IGNORE_UNKNOWN_CA;
InternetSetOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
// Set cookie data
if(!HttpAddRequestHeaders(hRequest, "Cookie: my_cookie=chocolate_chip\r\n", -1L, HTTP_ADDREQ_FLAG_ADD))
PrintError("HttpAddRequestHeaders");
return 0;
if(!HttpSendRequest(hRequest, NULL, 0, NULL, 0))
PrintError("HttpSendRequest");
return 0;
for(;;)
dwReadSize = 0;
InternetReadFile(hRequest, szBuffer + nBufferSize, sizeof(szBuffer) - nBufferSize - 1, &dwReadSize);
if(!dwReadSize)
break;
szBuffer[nBufferSize + dwReadSize] = 0;
nBufferSize += dwReadSize;
if(strstr(szBuffer, "chocolate_chip"))
bCookie = true;
OutputDebugString("Cookie Is Working...\n");
hRequest = HttpOpenRequest(hSession, "POST", "/index.php", "HTTP/1.1", NULL, NULL, INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES, NULL);
if(hRequest == NULL)
PrintError("HttpOpenRequest");
return 0;
// Need to do this to support self signed SSL certificates
InternetQueryOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
dwFlags = INTERNET_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_IGNORE_UNKNOWN_CA;
InternetSetOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
// Should be at the top, just for debug..
char *szPostData = "my_post=HelloWorld!";
if(!HttpSendRequest(hRequest, "Content-Type: application/x-www-form-urlencoded", -1, (LPVOID)szPostData, sizeof(szPostData)))
PrintError("HttpOpenRequest");
return 0;
// Clear the buffer from before
memset(szBuffer, 0, sizeof(szBuffer));
// Maybe there is a better way to read data?
for(;;)
dwReadSize = 0;
InternetReadFile(hRequest, szBuffer + nBufferSize, sizeof(szBuffer) - nBufferSize - 1, &dwReadSize);
if(!dwReadSize)
break;
szBuffer[nBufferSize + dwReadSize] = 0;
nBufferSize += dwReadSize;
MessageBox(0, szBuffer, 0, 0); // Always empty for post
InternetCloseHandle(hRequest);
InternetCloseHandle(hSession);
InternetCloseHandle(hInternet);
return 0;
我使用的 PHP 代码只是为了让您可以在这里看到我想要做什么。
<?php
if(isset($_COOKIE['my_cookie']))
echo $_COOKIE['my_cookie'];
if(isset($_POST['my_post']))
echo $_POST['my_post'];
?>
在这里待了几个小时。有谁知道怎么回事?
【问题讨论】:
应该重新标记 WinINet。开始查看服务器正在接收的原始正文。 重新标记。我检查了服务器日志。似乎还可以。 [26/Aug/2013:10:41:12 +1200] "POST /index.php HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/ 23.0" $_POST[] 似乎没有设置好。 【参考方案1】:char *szPostData = "my_post=HelloWorld!";
if(!HttpSendRequest(hRequest, "Content-Type: application/x-www-form-urlencoded", -1, (LPVOID)szPostData, sizeof(szPostData)))
sizeof(szPostData)
不会做你认为的那样。它计算char*
指针的字节大小,而不是它指向的数据。
【讨论】:
... 以及后续。将其更改为static const char szPostData[] = "..."
并从请求大小中减去 1。或者使用strlen()
,虽然这似乎有点没有意义,因为它总是相同的长度。以上是关于任何人都知道为啥这个带有 WinHttp 的 POST 失败了的主要内容,如果未能解决你的问题,请参考以下文章