VC实现http发送get和post请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shufac/article/details/72843962

VC实现http发送get和post请求

get请求

首先通过前面介绍的抓包工具获取请求的详细内容,然后再通过VC拼接Header,函数如下:
bool CXXX::http_get_eng_mode()
{
	//<?xml version=\"1.0\" encoding=\"US-ASCII\"?> 


<RGW><param><method>call</method><session>000</session><obj_path>cm</obj_path><obj_method>get_wan_configs</obj_me


thod></param></RGW>
	CString strURL=_T("http://")+loginfo.strIPAddress+_T("/xml_action.cgi?


method=get&module=duster&file=wan");
	bool bResult = false;
	DWORD dwType = 0;
	CString strServer;
	CString strObject;
	INTERNET_PORT wPort = 0;
	DWORD dwFileLength = 0;


	bResult =  AfxParseURL(strURL, dwType, strServer, strObject, wPort);
	if(!bResult)
	{
		return false;
	}
	int iCount=0;
	bool bRet=true;
	while (bRet)
	{
		if (iCount>2)
		{
			bRet=false;
		}
		try
		{
			CInternetSession Session ; 
			CHttpConnection *pHttpConnect = Session.GetHttpConnection(strServer, wPort) ;
			if(pHttpConnect)
			{
				CHttpFile* pFile = pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_GET, 
					_T("/xml_action.cgi?method=get&module=duster&file=wan")); //
				if ( pFile )
				{
					CString strAuthHeader=getAuthHeader


(loginfo.strAdmin,loginfo.strPassword,"GET");
					pFile->AddRequestHeaders(strAuthHeader);
					pFile->AddRequestHeaders("Expires: -1");					
					pFile->AddRequestHeaders("Accept: */*");					
					pFile->AddRequestHeaders("Cache-Control: no-store, no-cache, must-


revalidate");
					pFile->AddRequestHeaders("Pragma: no-cache");
					pFile->AddRequestHeaders("X-Requested-With: XMLHttpRequest");
					CString strTemp=_T("");strTemp=_T("Referer: 


http://")+loginfo.strIPAddress+_T("/index_pc.html");
					pFile->AddRequestHeaders(strTemp);
					pFile->AddRequestHeaders("Accept-Language: zh-CN");
					pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
					pFile->AddRequestHeaders("User-Agent: Mozilla/5.0 (Windows NT 6.1; 


Trident/7.0; rv:11.0) like Gecko");
					CString strHost=_T("Host: ")+loginfo.strIPAddress;
					pFile->AddRequestHeaders(strHost) ;
					pFile->AddRequestHeaders("Connection: Keep-Alive");
					pFile->AddRequestHeaders("Cookie: locale=en; platform=mifi") ;


					if (!pFile->SendRequest())
					{
						pFile->Close();
						pHttpConnect->Close();						
						return false;
					}
					
					DWORD dwStateCode = 0;
					pFile->QueryInfoStatusCode(dwStateCode);
					if(dwStateCode == HTTP_STATUS_OK) 
					{
						CString strInfo1;
						DWORD dw1=0;
						pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF ,strInfo1 ,&dw1) ;
						CString strInfo2;
						DWORD dw2=0;
						pFile->QueryInfo(HTTP_QUERY_CONTENT_BASE ,strInfo2 ,&dw2);
						char content_return[1024*10];memset(content_return,0,1024*10);
						pFile->Read(content_return, SERVER_CONTENT_LEN);
						
						//解析xml,查询工程模式
						CString str=getEngMode_1802(_bstr_t(content_return));
						if (!str.IsEmpty())
						{
							if (!IsEngModeOpen(0,str))
							{
								//如果没有开启的话,需要先打开
								http_set_eng_mode_1802(true);
							}
						}
						else
						{
							bResult=false;
						}						
					}
					else
					{
						bResult=false;
					}
					pFile->Close();
					delete pFile ;
					bRet=false;
				}
				pHttpConnect->Close() ;
				delete pHttpConnect ;
			}
		}
		catch( CInternetException *e )
		{
			e->Delete();    
		}
	}
	return bResult;
}

post请求

bool CXXX::http_set_eng_mode(bool bEnable)
{
	CString requestData=_T("");
	if (bEnable)
	{
		requestData = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?> 


<RGW><param><method>call</method><session>000</session><obj_path>cm</obj_path><obj_method>set_eng_mode</obj_metho


d></param><wan><mode>1</mode></wan></RGW>";
	}
	else
	{
		requestData = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?> 


<RGW><param><method>call</method><session>000</session><obj_path>cm</obj_path><obj_method>set_eng_mode</obj_metho


d></param><wan><mode>0</mode></wan></RGW>";
	}
	
	CString strURL=_T("http://")+loginfo.strIPAddress+_T("/xml_action.cgi?method=set");
	bool bResult = false;
	DWORD dwType = 0;
	CString strServer;
	CString strObject;
	INTERNET_PORT wPort = 0;
	DWORD dwFileLength = 0;


	bResult =  AfxParseURL(strURL, dwType, strServer, strObject, wPort);
	if(!bResult)
	{
		return false;
	}
	int iCount=0;
	bool bRet=true;
	while (bRet)
	{
		if (iCount>2)
		{
			bRet=false;
		}
		try
		{
			
			CInternetSession Session ; 
						
			CHttpConnection *pHttpConnect = Session.GetHttpConnection(strServer, wPort) ;
			if(pHttpConnect)
			{
				CHttpFile* pFile = pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_POST, 
					_T("/xml_action.cgi?method=set"),
					NULL,
					1,
					NULL,
					NULL,
					INTERNET_FLAG_NO_COOKIES); //
				if ( pFile )
				{					
					//打开工程模式
					pFile->AddRequestHeaders("Accept: application/xml, text/xml, */*; 


q=0.01");
					pFile->AddRequestHeaders("Content-Type: application/x-www-form-


urlencoded; charset=UTF-8");
					CString strAuthHeader=getAuthHeader


(loginfo.strAdmin,loginfo.strPassword,"POST");
					pFile->AddRequestHeaders(strAuthHeader);
					pFile->AddRequestHeaders("X-Requested-With: XMLHttpRequest");
					CString strTemp=_T("");strTemp=_T("Referer: 


http://")+loginfo.strIPAddress+_T("/index.html");
					pFile->AddRequestHeaders(strTemp);
					pFile->AddRequestHeaders("Accept-Language: zh-CN");
					pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
					pFile->AddRequestHeaders("User-Agent: Mozilla/5.0 (Windows NT 6.1; 


Trident/7.0; rv:11.0) like Gecko");
					CString strHost=_T("Host: ")+loginfo.strIPAddress;
					pFile->AddRequestHeaders(strHost);
					int iLength=requestData.GetLength();
					CString strLength=_T("");strLength.Format("Content-Length: %d",iLength);
				
					pFile->AddRequestHeaders(strLength);
					pFile->AddRequestHeaders("Connection: Keep-Alive");
					pFile->AddRequestHeaders("Pragma: no-cache");
					//Cookie: CGISID=8CeJlM1rEpSJSaCag78vaHYqoF9EewhmbnYo9QFF6zqZG
					
					CString strCookie1=_T("Cookie: ")+setcookie;
					pFile->AddRequestHeaders(strCookie1);


					if(!pFile->SendRequest(NULL,0,(void*)requestData.GetBuffer


(requestData.GetLength()), requestData.GetLength()))
					{
						pFile->Close();
						pHttpConnect->Close();						
						return false;
					}			
					DWORD dwStateCode = 0;
					pFile->QueryInfoStatusCode(dwStateCode);
					
					if(dwStateCode == HTTP_STATUS_OK) 
					{
						CString strInfo1;
						DWORD dw1=0;
						pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF ,strInfo1 ,&dw1);
						char content_return[1024*10];memset(content_return,0,1024*10);
						pFile->Read(content_return, SERVER_CONTENT_LEN);
					}
					pFile->Close();
					delete pFile ;
					bRet=false;
				}
				pHttpConnect->Close() ;
				delete pHttpConnect ;
			}
		}
		catch( CInternetException *e )
		{
			e->Delete();    
		}
	}
	return bResult;
}

较get请求而言,主要是除了Header外还需要post数据。


猜你喜欢

转载自blog.csdn.net/shufac/article/details/72843962