HOWTO: Simulate a Form POST Request Using WinInet Q165298
Here is a snippet of code that uses the MFC WinInet classes to simulate a Form POST request:
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());
Without MFC, the same code translates to straight SDK calls as follows: static TCHAR hdrs[] =
_T("Content-Type: application/x-www-form-urlencoded");
static TCHAR frmdata[] =
_T("name=John+Doe&userid=hithere&other=P%26Q");
statuc TCHAR accept[] =
_T("Accept: */*"); // 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
Here is a snippet of code that uses the MFC WinInet classes to simulate a Form POST request:
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());
Without MFC, the same code translates to straight SDK calls as follows: static TCHAR hdrs[] =
_T("Content-Type: application/x-www-form-urlencoded");
static TCHAR frmdata[] =
_T("name=John+Doe&userid=hithere&other=P%26Q");
statuc TCHAR accept[] =
_T("Accept: */*"); // 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
它直接提供一个方法Post(url, Data)
可以,我怎么也成功不了,不知道如何是好
login.mail.sohu.com在openrequest的szpath是/chkpwd.php我可以连上服务器!可是,我发送的数据是“UserName=myname&Password=mypwd&x=28&y=4"
但是,我用sniffer抓来的不是我要post!导致服务器说我的用户名是空!更在ie登陆时用空用户名登陆的效果是一样的!
CString m_optional=“UserName=myname&Password=mypwd&x=28&y=4"
CString szHeaders="Content-Type:application/x-www-form-urlencoded\r\nAccept-Encoding:gzip,deflate\r\nProxy-Connection:Keep-Alive\r\nUser-Agent:Mozilla/4.0(compatible;MSIE 5.01;Windows NT 5.0)";
//
BOOL bSendRequest=::HttpSendRequest(hHttpFile,
szHeaders, szHeaders.GetLength(),
&m_optional,
m_optional.GetLength());
帮忙看看有什么问题啊?
楼上的,你在那里指定的上传方式啊?
HttpSendRequest(....到底是Get, 还是Post?
post的数据必须是char *类型!(vc中)