我用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。

解决方案 »

  1.   

    你的HTTP头不对啊
    CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
    这句相当于将HTTP头里的其他信息都覆盖了你应该是希望在http头里面加这一行,用m_HttpFile->AddRequestHeaders()然后m_HttpFile->SendRequest(NULL,0,(LPVOID)form,17);
      

  2.   

    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());   
      

  3.   


    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);我用的一些代码,看有没有帮助