请教一下,VC 向web post数据哪位能讲一下,或者有例子 CInternetSession m_InetSession(_T("session"),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
CHttpConnection *pServer = NULL;
CHttpFile* pFile = NULL;
INTERNET_PORT port =8001;
try
{
pServer = m_InetSession.GetHttpConnection("127.0.0.1",port);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
CString strHeaders = "Accept: */*\r\nReferer: http://www.goodwaiter.com/\r\nAccept-Language: zh-cn\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
pFile->AddRequestHeaders(strHeaders);
pFile->SendRequestEx(m_strRequest.GetLength());
pFile->WriteString(m_strRequest);//重要-->m_Request 中有"name=aaa&name2=BBB&"
pFile->EndRequest();
/*DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
CString str;
m_Mutex.Lock();
m_strHtml="";
char szBuff[1024]; if (dwRet == HTTP_STATUS_OK)
{
UINT nRead;
while ((nRead = pFile->Read(szBuff,1023))>0)
{
m_strHtml += CString(szBuff,nRead);
}
}
m_Mutex.Unlock();
delete pFile;
delete pServer; */
}
catch (CInternetException* e)
{
CString s;
s.Format("Internet Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext);
AfxMessageBox(s);
//catch errors from WinInet
}这是我找到的,
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
pFile->WriteString(m_strRequest);//重要-->m_Request 中有"name=aaa&name2=BBB&"这两句里面的参数不是很清楚:"/" m_strRequest
CHttpConnection *pServer = NULL;
CHttpFile* pFile = NULL;
INTERNET_PORT port =8001;
try
{
pServer = m_InetSession.GetHttpConnection("127.0.0.1",port);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
CString strHeaders = "Accept: */*\r\nReferer: http://www.goodwaiter.com/\r\nAccept-Language: zh-cn\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
pFile->AddRequestHeaders(strHeaders);
pFile->SendRequestEx(m_strRequest.GetLength());
pFile->WriteString(m_strRequest);//重要-->m_Request 中有"name=aaa&name2=BBB&"
pFile->EndRequest();
/*DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
CString str;
m_Mutex.Lock();
m_strHtml="";
char szBuff[1024]; if (dwRet == HTTP_STATUS_OK)
{
UINT nRead;
while ((nRead = pFile->Read(szBuff,1023))>0)
{
m_strHtml += CString(szBuff,nRead);
}
}
m_Mutex.Unlock();
delete pFile;
delete pServer; */
}
catch (CInternetException* e)
{
CString s;
s.Format("Internet Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext);
AfxMessageBox(s);
//catch errors from WinInet
}这是我找到的,
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
pFile->WriteString(m_strRequest);//重要-->m_Request 中有"name=aaa&name2=BBB&"这两句里面的参数不是很清楚:"/" m_strRequest
http://support.microsoft.com/support/kb/articles/Q167/6/58.ASP
//****************************************************************************************//
//函数 PostContent
//主要功能: Post方式向服务器传数据
//参数列表:
//返回值:
//备注:
//****************************************************************************************//
bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &strDescript)
{
try{ strDescript = "提交成功完成!";
bool bRet = false;
CString strServer, strObject, strHeader, strRet;
unsigned short nPort;
DWORD dwServiceType;
if(!AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort))
{
strDescript = strUrl + "不是有效有网络地址!";
return false;
}
CInternetSession sess;//Create session CHttpFile* pFile;
//////////////////////////////////////////////
CHttpConnection *pServer = sess.GetHttpConnection(strServer, nPort);
if(pServer == NULL)
{
strDescript = "对不起,连接服务器失败!";
return false;
}
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObject,NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
if(pFile == NULL)
{
strDescript = "找不到网络地址" + strUrl;
return false;
}// pFile -> AddRequestHeaders("Content-Type: text/xml; charset=utf-8");
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
pFile -> SendRequest(NULL, 0, (LPTSTR)(LPCTSTR)strPara, strPara.GetLength()); CString strSentence;
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = pFile->QueryInfo(
HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
&dwStatus, &dwBuffLen); if( bSuccess && dwStatus>= 200 && dwStatus<300)
{
char buffer[2049];
memset(buffer, 0, 2049);
int nReadCount = 0;
while((nReadCount = pFile->Read(buffer, 2048)) > 0)
{
strContent += buffer;
memset(buffer, 0, 2049);
}
bRet = true;
}
else
{
strDescript = "网站服务器错误" + strUrl;
bRet = false;
}
////////////////////////////////////////
pFile->Close();
sess.Close();
return bRet;
}
catch(...)
{
int nCode = GetLastError();
strDescript.Format("向服务器post失败!错误号:%d", nCode);
return false;
}
}
_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());试问;
http://localhost:8001/image?upfile=E:\ABC.TXT&Submit=Press
FormActionHere 是不是"/image"?
if(!AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort))
通不过
0,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE); //ÉèÖò»»º³å
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strHtml = "";
CString strRequest = "name=123&pwd=321\r\n"; //POST¹ýÈ¥µÄÊý¾Ý
CString strHeaders = "Accept: */*\r\nReferer: http://www.goodwaiter.com/\r\nAccept-Language: zh-cn\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
try{
INTERNET_PORT nPort; //¶Ë¿Ú
nPort=80;
pServer = m_InetSession.GetHttpConnection("www.goodwaiter.com", nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
pFile->AddRequestHeaders(strHeaders);
pFile->SendRequestEx(strRequest.GetLength());
pFile->WriteString(strRequest); //ÖØÒª-->m_Request ÖÐÓÐ"name=aaa&name2=BBB&..."
pFile->EndRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_OK){
CString strLine;
CString nRead;
while ((nRead = pFile->ReadString(strLine))>0)
{
strHtml += strLine;
}
}
delete pFile;
delete pServer;
}
catch (CInternetException* e){
e->m_dwContext;
}
ACTION??
SERVER??
CString strServer,strObject;
INTERNET_PORT nPort;
DWORD dwServiceType= 0;
InternetGetConnectedState(&dwServiceType, 0);
这句里面的strUrl是做什么用的?第二个问题:
<FORM action='http://localhost:8001/cmd' method='post'>
实现这句代码CHttpConnection *pServer = sess.GetHttpConnection(strServer, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,_T("/cmd"));
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
pFile -> SendRequest(strHeaders,(LPTSTR)(LPCTSTR)"这里写参数", strPara.GetLength());
对吗?特别是那个Action
唉~ 你干嘛OpenRequest(CHttpConnection::HTTP_VERB_POST,_T("/cmd"));
将整个URL都放在strURL里面
结果返回:成功,且服务程序有反应
我用你给的代码,现在也可以返回成功了,但服务程序没有反应(8001这个HTTP服务是服务程序开的)是不是跟协议头什么的有关系如果可以请加我QQ 275782930非常感谢你!
抓Request这玩意得自己来,帮不了你。每个都不一样,要根据具体情况
请看,不知道页面POST的时候前面4条信息是什么,我用VC写的程序没有
抓包抓到的,并不一定都要Post
不过,除了传值还会传图片,请问传图片的话,该怎么办pFile->AddRequestHeaders("Accept: */*");
pFile->AddRequestHeaders("Accept-Language: zh-cn");
pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
pFile->AddRequestHeaders("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); 通过监控,我用pFile -> AddRequestHeaders()把他们设成一样,终于可以传值了,但传文件还不行,还得请你帮帮忙
发信息pFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
pFile->EndRequest(HSR_SYNC);
这个发文件,
这两句的作用是,我现在可以把数据发过去,但没有响应
貌似是Content-Type: multipart/form-data
后面你再EndRequest(HSR_SYNC);
{
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
pHTTP->AddRequestHeaders("Accept: */*");
pHTTP->AddRequestHeaders("Accept-Language: zh-cn");
pHTTP->AddRequestHeaders(_T("Content-Type: multipart/form-data; boundary=---------------------------7b4a6d158c9\r\n"));//发送包头请求
pHTTP->AddRequestHeaders("Accept-Encoding: gzip, deflate");
pHTTP->AddRequestHeaders("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
pHTTP->AddRequestHeaders("Connection: Keep-Alive");
pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
#ifdef _UNICODE
pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());//写入服务器所需信息
#endif
dwReadLength = -1;
while (0 != dwReadLength)
{
strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
TRACE(strDebugMessage);
dwReadLength = fTrack.Read(pBuffer, dwChunkLength);
if (0 != dwReadLength)
{
pHTTP->Write(pBuffer, dwReadLength);//写入服务器本地文件,用二进制进行传送
}
}
#ifdef _UNICODE
pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif
pHTTP->EndRequest(HSR_SYNC);
dwResponseLength = pHTTP->GetLength();
是这样的啊