哪位朋友用winhttp post过数据?咋string.c_str()不行啊?服务器收不到但Const char *却行. 本帖最后由 feethink 于 2010-03-10 21:37:15 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if (!WinHttpSendRequest(hRequest, szAdditionalHeader.c_str(), szAdditionalHeader.length(), (LPVOID)dd, 11,11, (DWORD_PTR)this)) { sStatus.szInfo=L"WinHttpSendRequest网址失败!"; goto cleanup; } 楼上的高人?szAdditionalHeader.length() sdk上边没这样说啊.再说那个是消息头.不关form数据的事吧.sdk上说只要设置为-1L.消息头会自动计算的.我试了你那样做.服务器一样没收到.. 这个函数bool CFTHttp::Get(LPWSTR szUrl,LPSTR fileName) { szFileName=fileName; bool bRet=false; wstring szAdditionalHeader=L"";//附加头部 string szFormData="";//表单数据 WCHAR szHost[256]; DWORD dwOpenRequestFlag = 0; URL_COMPONENTS urlComp; bool fRet = false; // Initialize URL_COMPONENTS structure. ZeroMemory(&urlComp, sizeof(urlComp)); urlComp.dwStructSize = sizeof(urlComp); // Use allocated buffer to store the Host Name. urlComp.lpszHostName = szHost; urlComp.dwHostNameLength = sizeof(szHost) / sizeof(szHost[0]); // Set non-zero lengths to obtain pointer a to the URL Path. // NOTE: If we threat this pointer as a null-terminated string, // it gives us access to additional information as well. urlComp.dwUrlPathLength = -1; // Crack HTTP scheme. urlComp.dwSchemeLength = -1; // Set the szMemo string. // Crack the URL. if (!WinHttpCrackUrl(szUrl, 0, 0, &urlComp)) { sStatus.szInfo=L"WinHttpCrackUrl分析URL失败.网址可能不正确"; goto cleanup; } hConnect = WinHttpConnect(hSession, szHost, urlComp.nPort, 0); if (NULL == hSession) { goto cleanup; } // Prepare OpenRequest flag if (sReqHeader.szVerb==L"POST") { szAdditionalHeader.append(L"Content-Type: application/x-www-form-urlencoded\r\n"); } dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0; hRequest=WinHttpOpenRequest(hConnect, sReqHeader.szVerb.c_str(), urlComp.lpszUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwOpenRequestFlag); if (0==hRequest) { sStatus.szInfo=L"WinHttpOpenRequest打开网址失败!"; goto cleanup; } // Install the status callback function. WINHTTP_STATUS_CALLBACK pCallback = WinHttpSetStatusCallback(hRequest, (WINHTTP_STATUS_CALLBACK)AsyncCallback, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_REDIRECT, NULL); // note: On success WinHttpSetStatusCallback returns the previously defined callback function. // Here it should be NULL if (pCallback != NULL) { goto cleanup; } //prepare additional header if (sReqHeader.cCookies!=NULL) { szAdditionalHeader.append(L"Cookie:"); szAdditionalHeader.append(sReqHeader.cCookies->CookiesStr); szAdditionalHeader.append(L"\r\n"); } if (sReqHeader.szReferer!=L"") { szAdditionalHeader.append(L"Referer:"); szAdditionalHeader.append(sReqHeader.szReferer); szAdditionalHeader.append(L"\r\n"); } szFormData.append("xxx=ddd\r\n"); const char *dd=szFormData.c_str();//"xx=ddd你好"; /* const char *d2=new char[7]; CopyMemory((void *)d2,(void *)dd,7);*/ //const wchar_t *dd=szFormData.c_str(); //dd=szFormData.c_str(); // Send the request. if (!WinHttpSendRequest(hRequest, szAdditionalHeader.c_str(),-1L, (void *)dd, 11,11, (DWORD_PTR)this)) { sStatus.szInfo=L"WinHttpSendRequest网址失败!"; goto cleanup; } bRet=true;cleanup: if (false==bRet) { Free(); } return bRet; }C#里边 protected override void Render(HtmlTextWriter writer) { // Response.ContentEncoding = System.Text.Encoding.UTF8; string reqHeader; reqHeader = Request.Headers.ToString(); Response.Write("request type\r\n" + Request.RequestType+"\r\n"); Response.Write("header is :\r\n"+reqHeader+"\r\n"); Response.Write("type is \r\n" + System.Text.Encoding.Default+ "\r\n"); // Response.Write(Request.Cookies[0].Name + "--" + Request.Cookies[0].Value+"\r\n|"); // Response.Write(Request.Cookies[1].Name + "--" + Request.Cookies[1].Value + "\r\n|"); // Response.Write("come from :" + Request.UrlReferrer + "\r\n"); Response.Write("form data count is:" + Request.Form.Count.ToString() + "\r\n"); Response.Write("form data count is:" + Request.Form.Keys[0] + "\r\n"); } 晚上开始就这问题.还没解决.太郁闷了.要不是操作频繁.我就copymemery算了...哎. 有没兴趣加我QQ5 777 63871远程帮我一把?我急用啊.我用的vs2010 win32 创建一个按钮以后怎么能响应点击呢? 盖茨退休了,而且捐出了全部财富,我只有佩服。。。。。。 100分求救 双缓冲录音类,如何才能减小噪音? 哪位大哥有ICO图标给小弟发一个,谢谢! 请问xp系统的boot(相当于98的boot.ini)文件在哪个文件夹下? MFC----------菜鸟的尴尬 请问如何模拟空格按键,让程序响应按键事件 使 按钮虚显??(散分了!) 请问如何调出颜色选择框,20分!!!很简单 菜鸟菜问 关于判断串口传输的DATA 新手怎样将16进制字符转成汉字
szAdditionalHeader.c_str(), szAdditionalHeader.length(),
(LPVOID)dd, 11,11,
(DWORD_PTR)this))
{
sStatus.szInfo=L"WinHttpSendRequest网址失败!";
goto cleanup;
}
bool CFTHttp::Get(LPWSTR szUrl,LPSTR fileName)
{
szFileName=fileName;
bool bRet=false;
wstring szAdditionalHeader=L"";//附加头部
string szFormData="";//表单数据
WCHAR szHost[256];
DWORD dwOpenRequestFlag = 0;
URL_COMPONENTS urlComp;
bool fRet = false; // Initialize URL_COMPONENTS structure.
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp); // Use allocated buffer to store the Host Name.
urlComp.lpszHostName = szHost;
urlComp.dwHostNameLength = sizeof(szHost) / sizeof(szHost[0]); // Set non-zero lengths to obtain pointer a to the URL Path.
// NOTE: If we threat this pointer as a null-terminated string,
// it gives us access to additional information as well.
urlComp.dwUrlPathLength = -1; // Crack HTTP scheme.
urlComp.dwSchemeLength = -1; // Set the szMemo string.
// Crack the URL.
if (!WinHttpCrackUrl(szUrl, 0, 0, &urlComp))
{
sStatus.szInfo=L"WinHttpCrackUrl分析URL失败.网址可能不正确";
goto cleanup;
}
hConnect = WinHttpConnect(hSession, szHost,
urlComp.nPort, 0);
if (NULL == hSession)
{
goto cleanup;
}
// Prepare OpenRequest flag
if (sReqHeader.szVerb==L"POST")
{
szAdditionalHeader.append(L"Content-Type: application/x-www-form-urlencoded\r\n");
}
dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;
hRequest=WinHttpOpenRequest(hConnect,
sReqHeader.szVerb.c_str(), urlComp.lpszUrlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag); if (0==hRequest)
{
sStatus.szInfo=L"WinHttpOpenRequest打开网址失败!";
goto cleanup;
}
// Install the status callback function.
WINHTTP_STATUS_CALLBACK pCallback = WinHttpSetStatusCallback(hRequest,
(WINHTTP_STATUS_CALLBACK)AsyncCallback,
WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS |
WINHTTP_CALLBACK_FLAG_REDIRECT,
NULL); // note: On success WinHttpSetStatusCallback returns the previously defined callback function.
// Here it should be NULL
if (pCallback != NULL)
{
goto cleanup;
} //prepare additional header
if (sReqHeader.cCookies!=NULL)
{
szAdditionalHeader.append(L"Cookie:");
szAdditionalHeader.append(sReqHeader.cCookies->CookiesStr);
szAdditionalHeader.append(L"\r\n");
}
if (sReqHeader.szReferer!=L"")
{
szAdditionalHeader.append(L"Referer:");
szAdditionalHeader.append(sReqHeader.szReferer);
szAdditionalHeader.append(L"\r\n");
} szFormData.append("xxx=ddd\r\n"); const char *dd=szFormData.c_str();//"xx=ddd你好"; /* const char *d2=new char[7];
CopyMemory((void *)d2,(void *)dd,7);*/
//const wchar_t *dd=szFormData.c_str();
//dd=szFormData.c_str();
// Send the request.
if (!WinHttpSendRequest(hRequest,
szAdditionalHeader.c_str(),-1L,
(void *)dd, 11,11,
(DWORD_PTR)this))
{
sStatus.szInfo=L"WinHttpSendRequest网址失败!";
goto cleanup;
}
bRet=true;cleanup:
if (false==bRet)
{
Free();
}
return bRet;
}C#里边
protected override void Render(HtmlTextWriter writer)
{
// Response.ContentEncoding = System.Text.Encoding.UTF8;
string reqHeader;
reqHeader = Request.Headers.ToString();
Response.Write("request type\r\n" + Request.RequestType+"\r\n");
Response.Write("header is :\r\n"+reqHeader+"\r\n");
Response.Write("type is \r\n" + System.Text.Encoding.Default+ "\r\n");
// Response.Write(Request.Cookies[0].Name + "--" + Request.Cookies[0].Value+"\r\n|");
// Response.Write(Request.Cookies[1].Name + "--" + Request.Cookies[1].Value + "\r\n|");
// Response.Write("come from :" + Request.UrlReferrer + "\r\n");
Response.Write("form data count is:" + Request.Form.Count.ToString() + "\r\n");
Response.Write("form data count is:" + Request.Form.Keys[0] + "\r\n");
}