資源簡介
自己封裝了一個winhttp類,里面包括了獲取時間戳,cookie操作。代理操作。頭信息操作。返回狀態碼,post,get,put,圖片下載,文檔下載,url編碼,unicode,ansi轉換,utf8編碼,等等操作。基本上所有的網絡操作都包括了,直接可用。

代碼片段和文件信息
#include?“StdAfx.h“
#include?“Winhttp.h“
//?const?IID?IID_IWinHttpRequest?=
//?{
//? 0x016fe2ec
//? 0xb2c8
//? 0x45f8
//? {0xb2?0x3b?0x39?0xe5?0x3a?0x75?0x39?0x6b}
//?};
//?const?IID?IID_IWinHttpRequest?=
//?{
//? 0x06f29373?0x5c5a?0x4b54
//? {0xb0?0x25?0x6e?0xf1?0xbf?0x8a?0xbf?0x0e}
//?};
CWinhttp::CWinhttp(void)
{
CoInitializeEx(NULL?COINIT_MULTITHREADED?);
m_pResponseBody?=?NULL;
const?IID?IID_IWinHttpRequest?=
{
0x06f29373
0x5c5a
0x4b54
{0xb0?0x25?0x6e?0xf1?0xbf?0x8a?0xbf?0x0e}
};
HRESULT?hr?=?m_pHttpReq.CreateInstance(__uuidof(WinHttpRequest));//實例化智能指針
if?(m_pHttpReq)
{
//設置為TRUE以啟用Passport身份驗證支持
COleVariant?varTrue?=?VARIANT_TRUE;
m_pHttpReq->PutOption(WinHttpRequestOption_EnablePassportAuthentication?varTrue);
//開啟https到http之間的重定向
WinhttpEnableHttpsToHttpRedirects(true);
//使用TLS1協議
//_variant_t?val?=?SslErrorFlag_Ignore_All;
m_pHttpReq->PutOption(WinHttpRequestOption_SslErrorIgnoreFlags?SslErrorFlag_Ignore_All);
//使用TLS1協議?需要tls1.2等協議。需要注釋掉這里。不在設置協議,
//COleVariant?varTLS1?=?(long)SecureProtocol_TLS1;
//m_pHttpReq->PutOption(WinHttpRequestOption_SecureProtocols?varTLS1);
?
COleVariant?varhttp?=?VARIANT_TRUE;
m_pHttpReq->PutOption(WinHttpRequestOption_EnableHttp1_1varhttp);
}
}
CWinhttp::~CWinhttp(void)
{
? if(m_pResponseBody)
? {
? delete?[]?m_pResponseBody;
? }
}
//需要訪問的網址?method?=?GET?PUT?HEAD?POST?strurl?網址
bool?CWinhttp::WinhttpOpen(CString?strUrlCString?strMethod__ENCODE?_codelong?nTimer)
{
//if(?SUCCEEDED(?hr?)?){...}?//?如果成功
//if(?FAILED(?hr?)?){...}?//?如果失敗
//設置超時?連接超時?發送超時?接收超時
m_encode?=?_code?;?//設置默認解碼?
m_pHttpReq->SetTimeouts(0?nTimer*1000?nTimer*1000?nTimer*1000);
HRESULT?hr?=?S_FALSE;
try
{
COleVariant?varhttp?=?VARIANT_FALSE;
hr?=?m_pHttpReq->Open((LPCTSTR)strMethod?(LPCTSTR)strUrlvarhttp);
if?(FAILED(hr))?{
return?false;
}
//插入host先
insterHostHeader(FindString(strUrl_T(“://“)_T(“/“)));
}catch?(_com_error?&e)
{
_bstr_t?bstrErr?=?e.Description();
//m_strLastError?=?(LPCTSTR)bstrErr;
return?false;
}
return?true;
}
//?添加訪問網址的頭信息?
bool?CWinhttp::WinhttpInsterHeader(CString?strTypeCString?strData)
{
//if(?SUCCEEDED(?hr?)?){...}?//?如果成功
//if(?FAILED(?hr?)?){...}?//?如果失敗
HRESULT?hr?=?S_FALSE;
try
{
hr?=m_pHttpReq->SetRequestHeader((_bstr_t)strType?(_bstr_t)strData);
if?(FAILED(hr))?{
return?false;
}
}catch?(_com_error?&e)
{
_bstr_t?bstrErr?=?e.Description();
//m_strLastError?=?(LPCTSTR)bstrErr;
return?false;
}
return?true;
}
//插入新的cookies信息
bool?CWinhttp::WinhttpInsterCookies(CString?strData)
{
return?WinhttpInsterHeader(_T(“cookie“)strData);
}
//?添加瀏覽器useragent信息
bool?CWinhttp::WinhttpInsterUserAgent(CString?strUserAgent)
{
try
{
????????
m_pHttpReq->PutOption(WinHttpRequestOption_UserAgentString(_bstr_t)strUserAgent);
}c
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-04?12:36??winhttp\
?????文件???????30919??2018-09-19?16:38??winhttp\Winhttp1.cpp
?????文件????????5840??2018-09-13?23:54??winhttp\Winhttp1.h
- 上一篇:flex的as3xls讀寫excel
- 下一篇:DNS發展簡介
評論
共有 條評論