如何通过使用 WinInet 模拟表单POST请求
答案:2 悬赏:0 手机版
解决时间 2021-04-13 19:44
- 提问者网友:原来太熟悉了会陌生
- 2021-04-12 22:38
如何通过使用 WinInet 模拟表单POST请求
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-04-12 22:49
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
// URL-encoded form variables -
// name = "John Doe", userid = "hithere", other = "P&Q"
CString strFormData = _T("name=John Doe&userid=hithere&other=P&Q");
CInternetSession session;
CHttpConnection* pConnection = session.GetHttpConnection(_T("ServerNameHere"));
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,_T("FormActionHere"));
BOOL result = pFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
// URL-encoded form variables -
// name = "John Doe", userid = "hithere", other = "P&Q"
CString strFormData = _T("name=John Doe&userid=hithere&other=P&Q");
CInternetSession session;
CHttpConnection* pConnection = session.GetHttpConnection(_T("ServerNameHere"));
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,_T("FormActionHere"));
BOOL result = pFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
全部回答
- 1楼网友:千夜
- 2021-04-13 00:16
为了正确模拟使用 WinInet 的表单提交,您需要发送标头指示正确的内容类型。对于表单,合适的的内容类型标头:
Content-Type: application/x-www-form-urlencoded
在许多情况下,如果未正确指定content-type内容,服务器将不响应。例如,IIS 3.0 的 Active Server Pages 组件实际检查专门为应用程序/x-www 的窗体-发此标头添加到"Request.Form"对象的窗体变量之前。此 MIME/内容的类型表示请求的数据的窗体变量编码的 URL 的列表。URL 编码意味着空格字符 (ASCII 32) 被编码为 +,特殊字符 (如!' 以十六进制形式编码 %21。
下面是代码的使用 MFC WinInet 类模拟表单 POST 请求段:
CString strHeaders =
_T("Content-Type: application/x-www-form-urlencoded");
// URL-encoded form variables -
// name = "John Doe", userid = "hithere", other = "P&Q"
CString strFormData = _T("name=John+Doe&userid=hithere&other=P%26Q");
CInternetSession session;
CHttpConnection* pConnection =
session.GetHttpConnection(_T("ServerNameHere"));
CHttpFile* pFile =
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
_T("FormActionHere"));
BOOL result = pFile->SendRequest(strHeaders,
(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
如果不使用 MFC,相同的代码会转换为直接的 SDK 调用,如下所示:
static TCHAR hdrs[] =
_T("Content-Type: application/x-www-form-urlencoded");
static TCHAR frmdata[] =
_T("name=John+Doe&userid=hithere&other=P%26Q");
static LPSTR accept[2]={"*/*", NULL};
// for clarity, error-checking has been removed
HINTERNET hSession = InternetOpen("MyAgent",
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
_T("FormActionHere"), NULL, NULL, accept, 0, 1);
HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
// close any valid internet-handles
回到顶端回到顶端 | 提供反馈
Collapse image参考
有关 URL 编码和格式的窗体 POST 请求的详细信息,请参阅 RFC 1866 8.2 节。
回到顶端回到顶端 | 提供反馈
Collapse image属性
文章编号: 165298 - 最后修改: 2013年9月29日 - 修订: 1.0
这篇文章中的信息适用于:
Microsoft Foundation Class Library 4.2 当用于
Microsoft Visual C++ 4.2 企业版
Microsoft Visual C++ 5.0 企业版
Microsoft Visual C++ 5.0 专业版
关键字:
kbhowto kbprogramming kbmt KB165298 KbMtzh
机器翻译
重要说明:本文是由 Microsoft 机器翻译软件进行的翻译并可能由 Microsoft 社区通过社区翻译机构(CTF)技术进行后期编辑,或可能是由人工进行的翻译。Microsoft 同时向您提供机器翻译、人工翻译及社区后期编辑的文章,以便对我们知识库中的所有文章以多种语言提供访问
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯