1. 程式人生 > >mfc http協議連線伺服器獲取資料

mfc http協議連線伺服器獲取資料

m_strServerIP 是伺服器地址,依次為鍵值,ret是伺服器返回到的資料。

CHttpClient conn;
CString msg;
                                msg =pDlg->m_strServerIP + _T("?");
conn.addParam(_T("type"),_T("21"));
conn.addParam(_T("action"),_T("getinfo"));
conn.addParam(_T("sn"),pDlg->strdatescan1);
conn.addParam(_T("lotid"),pDlg->m_strID );
conn.addParam(_T("uid"),pDlg->m_suid);
conn.addParam(_T("pwd"),pDlg->m_spwd);
conn.addParam(_T("nopwd"),pDlg->m_snopwd);

t1=0;
t2=0;
t1=GetTickCount();//程式段開始前取得系統執行時間(ms)  
ret = conn.doPost(msg);

t2=GetTickCount();

附CHttpClient 類

#pragma once
#include "wininet.h" 
#include "afxinet.h" 
#include <string>


// 操作成功


#define SUCCESS        0


// 操作失敗


#define FAILURE        1


// 操作超時 www.it165.net


#define OUTTIME        2






class CHttpClient
{
public: 
void addParam(CString name,CString value); 
CString doPost(CString href); 
CString doGet(CString href); 
CString CHttpClient::UTF8ToUnicode(char* UTF8);


//int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);


//int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);


CHttpClient(); 
virtual ~CHttpClient(); 
void Clear();
private: 
CString CONTENT; 
vector<char> pu8;
int CL; 
CStringList values; 
CStringList names; 




//int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);
//





//CInternetSession *m_pSession;


//CHttpConnection *m_pConnection;
//
//CHttpFile *m_pFile;

public:
};


#include "StdAfx.h"
#include "stdafx.h"
#include "HttpClient.h"
//#ifdef _DEBUG 
//#undef THIS_FILE 
//static char THIS_FILE[]=__FILE__; 
//#define new DEBUG_NEW 
//#endif 
#include<vector>
#include <string>
#define int32 __int32
using namespace std;
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 


#define  NORMAL_CONNECT             INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_PRAGMA_NOCACHE 


#define  SECURE_CONNECT             NORMAL_CONNECT | INTERNET_FLAG_SECURE  


#define  NORMAL_REQUEST             INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE 


#define  SECURE_REQUEST             NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID 
#define  BUFFER_SIZE       1024




CHttpClient::CHttpClient() 

//m_pSession = new CInternetSession;
//
//m_pConnection = NULL;
//
//m_pFile = NULL;





CHttpClient::~CHttpClient() 

//Clear();
//
//if(NULL != m_pSession)
//{
//
//m_pSession->Close();
//
//delete m_pSession;
//
//m_pSession = NULL;
//
//}





//void CHttpClient::Clear()
//{
//
//if(NULL != m_pFile)
//{
//
//m_pFile->Close();
//
//delete m_pFile;
//
//m_pFile = NULL;
//
//}
//
//if(NULL != m_pConnection)
//{
//
//m_pConnection->Close();
//
//delete m_pConnection;
//
//m_pConnection = NULL;
//
//}
//
//}
//
//
//int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)
//{
//
//CString strServer;
//
//CString strObject;
//
//DWORD dwServiceType;
//
//INTERNET_PORT nPort;
//
//strResponse = _T("");
//
// 
//
//AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
//
// 
//
//if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
//{
//
//return FAILURE;
//
//}
//
// 
//
//try
//{
//m_pConnection = m_pSession->GetHttpConnection(strServer,
//
//dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,nPort);
//
//m_pFile = m_pConnection->OpenRequest(strMethod, strObject,
//NULL, 1, NULL, NULL,
//(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
//
// 
//
////DWORD dwFlags;
//
////m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
//
////dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
//
//////set web server option
//
////m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
//
// 
//
//m_pFile->AddRequestHeaders(_T("Accept: *,*/*"));
//
//m_pFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));
//
//m_pFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
//
//m_pFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate"));
//
//m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
//
// 
//
//char szChars[BUFFER_SIZE + 1] = {0};
//
//CString strRawResponse = _T("");
//
//UINT nReaded = 0;
//
//while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
//{
//
//szChars[nReaded] = '\0';
//
//strRawResponse += szChars;
//
//memset(szChars, 0, BUFFER_SIZE + 1);
//
//}
//
// 
//
//int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse, -1, NULL, 0);
//
//WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
//
//memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
//
// 
//
//MultiByteToWideChar(CP_UTF8,0,strRawResponse/*.c_str()*/,-1, pUnicode,unicodeLen);
//
//CString cs(pUnicode);
//
//delete [] pUnicode;
//
//pUnicode = NULL;
//
// 
//
//strResponse = cs;
//
//Clear();
//
//}
//catch (CInternetException* e)
//{
//
//Clear();
//
//DWORD dwErrorCode = e->m_dwError;
//
//e->Delete();
//
//DWORD dwError = GetLastError();
//
////PRINT_LOG("dwError = %d", dwError, 0);
//
//if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
//{
//
//return OUTTIME;
//
//}
//else
//{
//return FAILURE;
//}
//
//}
//
//return SUCCESS;
//
//}


 


//int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)
//
//{
//
//return ExecuteRequest("GET", strUrl, strPostData, strResponse);
//
//}
//
// 
//
//int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)
//
//{
//
//return ExecuteRequest("POST", strUrl, strPostData, strResponse);
//
//}






CString CHttpClient::doGet(CString href) 

CString httpsource=_T(""); 
CInternetSession session1(NULL,0,PRE_CONFIG_INTERNET_ACCESS,NULL,NULL,INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE );
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+_T("\r\n"); 



pHTTPFile->Close();
session1.Close();
delete pHTTPFile; 
pHTTPFile=NULL;
}else 




return httpsource; 



//CString CHttpClient::doPost(CString href) 
//{ 
//CString httpsource=_T(""); 
//CInternetSession session1; 
//CHttpConnection* conn1=NULL; 
//CHttpFile* pFile = NULL; 
//CString strServerName; 
//CString strObject; 
//INTERNET_PORT nPort; 
//DWORD dwServiceType; 
//
//
//AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort); 
//
////if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
////{
////
////return;
////}
//
//
//
//DWORD retcode; 
//char* outBuff = CONTENT.GetBuffer(1000); 
//
//try 
//{ 
//conn1 = session1.GetHttpConnection(strServerName,nPort);
//
//pFile = conn1->OpenRequest(0,strObject,NULL,1,NULL,_T("HTTP/1.1"),INTERNET_FLAG_EXISTING_CONNECT/*|INTERNET_FLAG_NO_AUTO_REDIRECT*/ | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE); 
//
////pFile = conn1->OpenRequest(0, strObject,
////NULL, 1, NULL, NULL,
////(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
//
//pFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded")); 
//pFile->AddRequestHeaders(_T("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+_T("\r\n"); 
//} 
//pFile->Close();
//
////delete pFile; 
////pFile = NULL;
//}else 
//{ 
//
//} 
//
//conn1->Close();
//delete conn1;
//conn1 = NULL;
//session1.Close(); 
//
////outBuff = NULL;
//
//return httpsource;
//} 


CString CHttpClient::doPost(CString href) 

CString httpsource=_T(""); 
CInternetSession session1; 
CHttpConnection* conn1=NULL; 
CHttpFile* pFile = NULL; 
CString strServerName; //ip?
CString strObject; 
INTERNET_PORT nPort; 
DWORD dwServiceType; 
AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort); 
DWORD retcode; 


//char* outBuff= new char(1000); 


char* pmb= new char(CONTENT.GetLength()+1);
strcpy(pmb,CONTENT.GetBuffer());
CONTENT.ReleaseBuffer(-1);
MBToUTF8(pu8, pmb,(CONTENT.GetLength()+1 )) ;
char* outBuff=&pu8[0];
pmb = NULL;
delete [] pmb;
//delete pmb;


//CONTENT.GetBuffer(1000);=UnicodeToUtf8(CONTENT); 


try 

session1.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,3000);
conn1 = session1.GetHttpConnection(strServerName,nPort); 
pFile = conn1->OpenRequest(0,strObject,NULL,1,NULL,_T("HTTP/1.1"),INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_DONT_CACHE ); 
pFile -> AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded")); 
pFile -> AddRequestHeaders(_T("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+_T("\r\n"); 

pFile->Close(); 
}else 






Clear();
 
delete pFile; 
delete conn1; 
session1.Close(); 
return httpsource;







void CHttpClient::addParam(CString name, CString value) 

names.AddTail((LPCTSTR)name); 
values.AddTail((LPCTSTR)value); 
CString eq=_T("="); 
CString an=_T("&"); 
//if(name == _T("imei") || name == _T("lotid"))
//CONTENT=CONTENT+name+eq+value;
//else
CONTENT=CONTENT+name+eq+value+an; 
CL=CONTENT.GetLength(); 





void CHttpClient::Clear()
{
CONTENT = _T("");
}