VC 向web post数据
请教一下,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
[解决办法]
http://support.microsoft.com/default.aspx?scid=kb;EN-US;165298
http://support.microsoft.com/support/kb/articles/Q167/6/58.ASP
[解决办法]
- C/C++ code
//****************************************************************************************////函数 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; }}
[解决办法]
CInternetSession m_InetSession(_T("session"),
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;
}[code=C/C++][/code]