C++调用HTTP实现方式.docx
- 文档编号:14544044
- 上传时间:2023-06-24
- 格式:DOCX
- 页数:10
- 大小:16.97KB
C++调用HTTP实现方式.docx
《C++调用HTTP实现方式.docx》由会员分享,可在线阅读,更多相关《C++调用HTTP实现方式.docx(10页珍藏版)》请在冰点文库上搜索。
C++调用HTTP实现方式
C++调用HTTP实现方式
2011-09-2417:
00:
52| 分类:
C++|字号 订阅
Http访问有两种方式,GET和POST,就编程来说GET方式相对简单点,它不用向服务器提交数据,程序中使用POST方式,
提交数据并从服务器获取返回值。
为实现Http访问,微软提供了二套API:
WinINet,WinHTTP。
WinHTTP比WinINet更加安全和健壮,可以这么认为WinHTTP是WinINet的升级版本。
程序中,通过一个宏的设置来决定是使用WinHttp还是WinINet。
#defineUSE_WINHTTP //Commentthislinetouserwininet.
下面来说说实现Http访问的流程(两套API都一样的流程):
1,首先我们打开一个Session获得一个HINTERNETsession句柄;
2,然后我们使用这个session句柄与服务器连接得到一个HINTERNETconnect句柄;
3,然后我们使用这个connect句柄来打开Http请求得到一个HINTERNETrequest句柄;
4,这时我们就可以使用这个request句柄来发送数据与读取从服务器返回的数据;
5,最后依次关闭request,connect,session句柄。
/***********************定义HTTP发送所用方法***********************************/
HINTERNETOpenSession(LPCWSTRuserAgent=0)
{
#ifdefUSE_WINHTTP
returnWinHttpOpen(userAgent,NULL,NULL,NULL,NULL);;
#else
returnInternetOpen(userAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
#endif
}
HINTERNETConnect(HINTERNEThSession,LPCWSTRserverAddr,intportNo)
{
#ifdefUSE_WINHTTP
returnWinHttpConnect(hSession,serverAddr,(INTERNET_PORT)portNo,0);
#else
returnInternetConnect(hSession,serverAddr,portNo,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
#endif
}
HINTERNETOpenRequest(HINTERNEThConnect,LPCWSTRverb,LPCWSTRobjectName,intscheme)
{
DWORDflags=0;
#ifdefUSE_WINHTTP
if(scheme==INTERNET_SCHEME_HTTPS){
flags|=WINHTTP_FLAG_SECURE;
}
returnWinHttpOpenRequest(hConnect,verb,objectName,NULL,NULL,NULL,flags);
#else
if(scheme==INTERNET_SCHEME_HTTPS){
flags|=INTERNET_FLAG_SECURE;
}
returnHttpOpenRequest(hConnect,verb,objectName,NULL,NULL,NULL,flags,0);
#endif
}
BOOLAddRequestHeaders(HINTERNEThRequest,LPCWSTRheader)
{
SIZE_Tlen=lstrlenW(header);
#ifdefUSE_WINHTTP
returnWinHttpAddRequestHeaders(hRequest,header,DWORD(len),WINHTTP_ADDREQ_FLAG_ADD);
#else
returnHttpAddRequestHeaders(hRequest,header,DWORD(len),HTTP_ADDREQ_FLAG_ADD);
#endif
}
BOOLSendRequest(HINTERNEThRequest,constvoid*body,DWORDsize)
{
#ifdefUSE_WINHTTP
returnWinHttpSendRequest(hRequest,0,0,const_cast
#else
returnHttpSendRequest(hRequest,0,0,const_cast
#endif
}
BOOLEndRequest(HINTERNEThRequest)
{
#ifdefUSE_WINHTTP
returnWinHttpReceiveResponse(hRequest,0);
#else
//ifyouuseHttpSendRequestExtosendrequestthenuseHttpEndRequestinhere!
returnTRUE;
#endif
}
BOOLQueryInfo(HINTERNEThRequest,intqueryId,char*szBuf,DWORD*pdwSize)
{
#ifdefUSE_WINHTTP
returnWinHttpQueryHeaders(hRequest,(DWORD)queryId,0,szBuf,pdwSize,0);
#else
returnHttpQueryInfo(hRequest,queryId,szBuf,pdwSize,0);
#endif
}
BOOLReadData(HINTERNEThRequest,void*buffer,DWORDlength,DWORD*cbRead)
{
#ifdefUSE_WINHTTP
returnWinHttpReadData(hRequest,buffer,length,cbRead);
#else
returnInternetReadFile(hRequest,buffer,length,cbRead);
#endif
}
voidCloseInternetHandle(HINTERNEThInternet)
{
if(hInternet)
{
#ifdefUSE_WINHTTP
WinHttpCloseHandle(hInternet);
#else
InternetCloseHandle(hInternet);
#endif
}
}
/**********************************************************/
///通过Http方式发送短信
stringSendSMS_HTTP(constlongececcid,conststring&password,conststring&msisdn,conststring&smsContent)
{
stringrtnStr="-1";
HINTERNEThSession=0;
HINTERNEThConnect=0;
HINTERNEThRequest=0;
wstringstrHeader(L"Content-type:
application/x-www-form-urlencoded\r\n");
//Testdata
CrackedUrlcrackedUrl(L"
stringStrPostData="ececcid=600000&password="+password+"&msisdn="+msisdn+"&smscontent="+smsContent+"&msgtype=5&longcode=";
StrPostData=string_To_UTF8(StrPostData);
//Opensession.
hSession=OpenSession(L"HttpPostby lyz_sea@");
if(hSession==NULL){
cout<<"Error:
Opensession!
\n";
return"-1";
}
//Connect.
hConnect=Connect(hSession,crackedUrl.GetHostName(),crackedUrl.GetPort());
if(hConnect==NULL){
cout<<"Error:
Connectfailed!
\n";
return"-1";
}
//Openrequest.
hRequest=OpenRequest(hConnect,L"POST",crackedUrl.GetPath(),crackedUrl.GetScheme());
if(hRequest==NULL){
cout<<"Error:
OpenRequestfailed!
\n";
return"-1";
}
//Addrequestheader.
if(!
AddRequestHeaders(hRequest,strHeader.c_str())){
cout<<"Error:
AddRequestHeadersfailed!
\n";
return"-1";
}
//Sendpostdata.
if(!
SendRequest(hRequest,StrPostData.c_str(),StrPostData.length())){
cout<<"Error:
SendRequestfailed!
\n";
return"-1";
}
//Endrequest
if(!
EndRequest(hRequest)){
cout<<"Error:
EndRequestfailed!
\n";
return"-1";
}
charszBuf[BUF_SIZE];
DWORDdwSize=0;
szBuf[0]=0;
//Queryheaderinfo.
#ifdefUSE_WINHTTP
intcontextLengthId=WINHTTP_QUERY_CONTENT_LENGTH;
intstatusCodeId=WINHTTP_QUERY_STATUS_CODE;
intstatusTextId=WINHTTP_QUERY_STATUS_TEXT;
#else
intcontextLengthId=HTTP_QUERY_CONTENT_LENGTH;
intstatusCodeId=HTTP_QUERY_STATUS_CODE;
intstatusTextId=HTTP_QUERY_STATUS_TEXT;
#endif
dwSize=BUF_SIZE;
if(QueryInfo(hRequest,contextLengthId,szBuf,&dwSize)){
szBuf[dwSize]=0;
cout<<"Contentlength:
"< } dwSize=BUF_SIZE; if(QueryInfo(hRequest,statusCodeId,szBuf,&dwSize)){ szBuf[dwSize]=0; cout<<"Statuscode: "< } dwSize=BUF_SIZE; if(QueryInfo(hRequest,statusTextId,szBuf,&dwSize)){ szBuf[dwSize]=0; cout<<"Statustext: "< } //readdata. for(;;){ dwSize=BUF_SIZE; if(ReadData(hRequest,szBuf,dwSize,&dwSize)==FALSE){ break; } if(dwSize<=0){ break; } szBuf[dwSize]=0; rtnStr=: : UTF8_To_string(string(szBuf)); cout< } CloseInternetHandle(hRequest); CloseInternetHandle(hConnect); CloseInternetHandle(hSession); return rtnStr; } 以上方法中用到的CrackURL方法在以下CrackURL.h文件中: #pragmaonce //#include //usingnamespacestd; #defineUSE_WINHTTP //Commentthislinetouserwininet. #ifdefUSE_WINHTTP #include #pragmacomment(lib,"winhttp.lib") #else #include #pragmacomment(lib,"wininet.lib") #endif //CrackedUrl classCrackedUrl{ intm_scheme; wstringm_host; intm_port; wstringm_path; public: CrackedUrl(LPCWSTRurl) { URL_COMPONENTSuc={0}; uc.dwStructSize=sizeof(uc); constDWORDBUF_LEN=256; WCHARhost[BUF_LEN]; uc.lpszHostName=host; uc.dwHostNameLength=BUF_LEN; WCHARpath[BUF_LEN]; uc.lpszUrlPath=path; uc.dwUrlPathLength=BUF_LEN; WCHARextra[BUF_LEN]; uc.lpszExtraInfo=extra; uc.dwExtraInfoLength=BUF_LEN; #ifdefUSE_WINHTTP if(! WinHttpCrackUrl(url,0,ICU_ESCAPE,&uc)){ cout<<"Error: WinHttpCrackUrlfailed! \n"; } #else if(! InternetCrackUrl(url,0,ICU_ESCAPE,&uc)){ printf("Error: InternetCrackUrlfailed! \n"); } #endif m_scheme=uc.nScheme; m_host=host; m_port=uc.nPort; m_path=path; } intGetScheme()const { returnm_scheme; } LPCWSTRGetHostName()const { returnm_host.c_str(); } intGetPort()const { returnm_port; } LPCWSTRGetPath()const { returnm_path.c_str(); } staticstringUrlEncode(constchar*p) { if(p==0){ returnstring(); } stringbuf; for(;;){ intch=(BYTE)(*(p++)); if(ch=='\0'){ break; } if(isalnum(ch)||ch=='_'||ch=='-'||ch=='.'){ buf+=(char)ch; } elseif(ch==''){ buf+='+'; } else{ charc[16]; wsprintfA(c,"%%%02X",ch); buf+=c; } } returnbuf; } };
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 调用 HTTP 实现 方式