c++使用HTTP网络通信教程

  • 时间:
  • 来源:互联网
  • 文章标签:
#include "HttpClient.h"
CString strResultStr;
CInternetSession session;
CHttpConnection *pConnection;
CHttpFile *pFile;
BOOL retFlag;

//与服务器建立连接
INTERNET_PORT nPort = 8080;
pConnection = session.GetHttpConnection( "192.168.10.173",nPort);
CString strHeaders, tempStr;
strHeaders = _T("Content-Type: application/x-www-form-urlencoded");

//登录页面为 www.XXXXX.com/Client/Login.aspx
pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, "/PaymentManager/cmbcnotify");
CString strUserinfo;
//在登录页面中主要有两个输入控件,为TextBoxUserName、TextBoxPassword,分别输入用户名和密码
strUserinfo.Format( "TextBoxUserName=1111&TextBoxPassword=1111");
BOOL result = pFile->SendRequest(
strHeaders,
(LPVOID)(LPCTSTR)strUserinfo, strUserinfo.GetLength() );

//将登录验证后服务器返回的信息放入strResultStr中
do
{
pFile->SetReadBufferSize(10*1024);
retFlag = pFile->ReadString(tempStr);
strResultStr += tempStr + "/n";
}while(retFlag != FALSE);

HTTP网络通信流程
1、创建CInternetSession对象
CInternetSession session;
2、创建CHttpConnection 对象
CHttpConnection*   pConnection;
1)域名方式:
 pConnection= pSession>GetHttpConnection(csServerName);
2)IP+端口号方式:
INTERNET_PORT nPort = 8080;
pConnection = session.GetHttpConnection( "192.168.10.173",nPort);
3、


本文链接http://www.taodudu.cc/news/show-647527.html