httpaddrequestheaders
场景:怎么利用HttpOpenRequest,HttpAddRequestHeaders,HttpSendRequestEx这三个函数发送POST请求
如何利用HttpOpenRequest,HttpAddRequestHeaders,HttpSendRequestEx这三个函数发送POST请求?
如何利用HttpOpenRequest,HttpAddRequestHeaders,HttpSendRequestEx这三个函数发送POST请求?
我准备使用POST请求做个登录操作。
根据调研需要用到这几个函数,但是我在简单配置HTTP header的时候,如果简单的使用一句Content-type:application/x-www-form-urlencoded的时候,HttpAddRequestHeaders可以执行成功。
但我还想加入其他的信息,比如帐号和密码信息,用POST命令方式登录,但是,配置之后,此海曙怎么也无法执行成功。错误提示是函数参数错误。
请问有没有朋友做过此方面的?
------解决方案--------------------
如何利用HttpOpenRequest,HttpAddRequestHeaders,HttpSendRequestEx这三个函数发送POST请求?
如何利用HttpOpenRequest,HttpAddRequestHeaders,HttpSendRequestEx这三个函数发送POST请求?
我准备使用POST请求做个登录操作。
根据调研需要用到这几个函数,但是我在简单配置HTTP header的时候,如果简单的使用一句Content-type:application/x-www-form-urlencoded的时候,HttpAddRequestHeaders可以执行成功。
但我还想加入其他的信息,比如帐号和密码信息,用POST命令方式登录,但是,配置之后,此海曙怎么也无法执行成功。错误提示是函数参数错误。
请问有没有朋友做过此方面的?
------解决方案--------------------
- C/C++ code
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; } }