CHttpConnection POST数据格式怎么写

richiezhao 2009-09-18 04:26:20
我用CHttpConnection以post的方式连接web服务器,服务器显示连接上了,但是post的数据deviceName没取到值,以下是我的连接服务器发送请求的代码:
m_HttpServerConn = m_ConnectSession.GetHttpConnection(strServerName,nPort);
m_HttpFile = m_HttpServerConn->OpenRequest(CHttpConnection::HTTP_VERB_POST,
strObject, NULL, 1, NULL, NULL, DWHTTPREQUESTFLAGS);
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
char form[] = "deviceName=aaaaa"; //post发送的参数,但服务端得不到aaaaa这个值。
m_HttpFile->SendRequest(strHeaders,(LPVOID)form,17);
DWORD dwRet;
m_HttpFile->QueryInfoStatusCode(dwRet);

请问这个是怎么回事,是不是我post的数据格式有问题。我的web应用服务器是用tomcat。
...全文
175 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
richiezhao 2009-09-18
  • 打赏
  • 举报
回复
这个刚试过了,不过还是不行,服务端报错了,那个参数能看到,但是参数的值没有。
CoderOfVC 2009-09-18
  • 打赏
  • 举报
回复

pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, //请求服务器
strObject,NULL,1,
NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_RELOAD);
if(pFile == NULL) //如果请求失败的话
{
SetLastErrorMsg();
return CF_RET_NETERROR;
}
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
pFile -> SendRequest(NULL, 0, (LPTSTR)(LPCTSTR)strPostData, strPostData.GetLength()); //POST数据

BOOL bSuccess = pFile->QueryInfo( //查询POST操作是否成功
HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
&dwStatus, &dwBuffLen);



我用的一些代码,看有没有帮助
MoXiaoRab 2009-09-18
  • 打赏
  • 举报
回复
CInternetSession   m_winet(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);   
CHttpConnection *pConnection;
CHttpFile *pFile;
pConnection = m_winet.GetHttpConnection("expert.csdn.net");

CString strHeaders, tempStr,str;
str="name=*******&pass=******&type=1";
//strHeaders = _T("Content-Type: application/x-www-form-urlencoded");

pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
"/member/logon.asp");
pFile->AddRequestHeaders("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
pFile->AddRequestHeaders("User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile->SendRequest(
NULL,0,
(LPVOID)(LPCTSTR)str, str.GetLength());

fangle6688 2009-09-18
  • 打赏
  • 举报
回复
你的HTTP头不对啊
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
这句相当于将HTTP头里的其他信息都覆盖了

你应该是希望在http头里面加这一行,用m_HttpFile->AddRequestHeaders(。。。)

然后m_HttpFile->SendRequest(NULL,0,(LPVOID)form,17);

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧