VC++ Http Get Post封装类

/************************Http: Get、Post的封装类***************/

#include   "wininet.h"  
#include   "afxinet.h"

/*********************类定义***********************************/
class   CHttpClient      
{  
public:  
	void   addParam(CString   name,CString   value);  
	CString   doPost(CString   href);  
	CString   doGet(CString   href);  
	CHttpClient();  
	virtual   ~CHttpClient();  
private:  
	CString   CONTENT;  
	int   CL;  
	CStringList   values;  
	CStringList   names;  
};  
 
/*************************类实现********************************/
// 添加字段
void   CHttpClient::addParam(CString   name,   CString   value)  
{  
	names.AddTail((LPCTSTR)name);  
	values.AddTail((LPCTSTR)value);  
	CString   eq="=";  
	CString   an="&";  
	CONTENT=CONTENT+name+eq+value+an;  
	CL=CONTENT.GetLength();  
}   
// 以http Get方式请求URL
CString   CHttpClient::doGet(CString   href)  
{  
	CString   httpsource="";  
	CInternetSession   session1(NULL,0);  
	CHttpFile*   pHTTPFile=NULL;  
	try{  
		pHTTPFile=(CHttpFile*)session1.OpenURL(href);  
		//session1.  
	}
	catch(CInternetException)
	{  
		pHTTPFile=NULL;  
	}  
	if(pHTTPFile)  
	{  
		CString   text;  
		for(int   i=0;pHTTPFile->ReadString(text);i++)  
		{  
			httpsource=httpsource+text+"\r\n";  
		}  
		pHTTPFile->Close();  
		delete   pHTTPFile;  
	}else  
	{  
		
	}  
	return   httpsource;  
}  
// 以Http Post方式请求URL
CString   CHttpClient::doPost(CString   href)  
{  
	CString   httpsource="";  
	CInternetSession   session1;  
	CHttpConnection*   conn1=NULL;  
	CHttpFile*   pFile   =   NULL;      
	CString   strServerName;  
	CString   strObject;  
	INTERNET_PORT   nPort;  
	DWORD   dwServiceType;  
	AfxParseURL((LPCTSTR)href, dwServiceType, strServerName, strObject, nPort);  
	DWORD   retcode;              
	char*   outBuff   =   CONTENT.GetBuffer(1000);  
	try      
	{                  
		conn1   =   session1.GetHttpConnection(strServerName, nPort);          
		pFile   =   conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);          
		pFile   ->   AddRequestHeaders("Content-Type:   application/x-www-form-urlencoded");          
		pFile   ->   AddRequestHeaders("Accept:   */*");                          
		pFile   ->   SendRequest(NULL,0,outBuff,strlen(outBuff)+1);                  
		pFile   ->   QueryInfoStatusCode(retcode);                  
	}                            
	catch   (CInternetException   *   e){};                  
	if(pFile)  
	{  
		CString   text;  
		for(int   i=0;pFile->ReadString(text);i++)  
		{  
			httpsource=httpsource+text+"\r\n";  
		}  
		pFile->Close();  
	}else  
	{  
		// do anything.....
	}  
	return   httpsource;  
	delete   pFile;          
	delete   conn1;          
	session1.Close();  
}  

// 使用范例
int main ()
{
  //get方式  
  CHttpClient   conn;  
  CString   value="http://xxx.com/test.asp";  
  value=conn.doGet(value);  

  //post方式  
  CHttpClient   conn;  
  value="http://xxx.com/test.asp";  
  conn.addParam("name1","value1");  
  conn.addParam("name2","value2");  
  conn.addParam("name3","value3");  
  value=conn.doPost(value);   
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值