|
@@ -3,10 +3,20 @@
|
|
|
|
|
|
HTTPHelper::HTTPHelper(void):m_hSession(NULL),m_hConnect(NULL),m_hRequest(NULL),m_nRetryTimes(3)
|
|
HTTPHelper::HTTPHelper(void):m_hSession(NULL),m_hConnect(NULL),m_hRequest(NULL),m_nRetryTimes(3)
|
|
{
|
|
{
|
|
|
|
+ /* 设置默认头值; */
|
|
|
|
+ AddRequestHeaders(L"Accept", L"*/*");
|
|
|
|
+ AddRequestHeaders(L"Connection", L"close");
|
|
|
|
+ AddRequestHeaders(L"Accept-Charset", L"UTF-8");
|
|
|
|
+ AddRequestHeaders(L"Cache-Control", L"no-cache");
|
|
|
|
+ AddRequestHeaders(L"User-Agent", L"WINHTTP-C++/1.0.01");
|
|
|
|
+ //AddRequestHeaders(L"Content-Type", L"application/json;charset=utf-8");
|
|
}
|
|
}
|
|
|
|
|
|
HTTPHelper::~HTTPHelper(void)
|
|
HTTPHelper::~HTTPHelper(void)
|
|
{
|
|
{
|
|
|
|
+ if (NULL != m_hRequest) WinHttpCloseHandle(m_hRequest);
|
|
|
|
+ if (NULL != m_hConnect) WinHttpCloseHandle(m_hConnect);
|
|
|
|
+ if (NULL != m_hSession) WinHttpCloseHandle(m_hSession);
|
|
}
|
|
}
|
|
|
|
|
|
bool HTTPHelper::UrlParse(std::wstring strUrl)
|
|
bool HTTPHelper::UrlParse(std::wstring strUrl)
|
|
@@ -47,9 +57,9 @@ bool HTTPHelper::UrlParse(std::wstring strUrl)
|
|
return FALSE;
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
|
|
-bool HTTPHelper::GetHtml(HTTPITEM &item, HTTPRESULT &result)
|
|
|
|
|
|
+bool HTTPHelper::GetHtml(HTTPRESULT &result)
|
|
{
|
|
{
|
|
- if ( !SendRequest(item,result) )
|
|
|
|
|
|
+ if ( !SendRequest(result) )
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return true;
|
|
return true;
|
|
@@ -84,21 +94,27 @@ BOOL HTTPHelper::_OpenHTTP()
|
|
return FALSE;
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 设置请求参数;
|
|
std::wstring strObjctName = m_strApiName;
|
|
std::wstring strObjctName = m_strApiName;
|
|
if ( m_strQueryParams.size() ) {
|
|
if ( m_strQueryParams.size() ) {
|
|
strObjctName.append(L"?");
|
|
strObjctName.append(L"?");
|
|
strObjctName.append(m_strQueryParams);
|
|
strObjctName.append(m_strQueryParams);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ DWORD dwFlags = WINHTTP_FLAG_REFRESH;
|
|
// 请求类型:是否是https;
|
|
// 请求类型:是否是https;
|
|
- DWORD dwOpenRequestFlag = (m_nScheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0;
|
|
|
|
|
|
+ if ( m_nScheme == INTERNET_SCHEME_HTTPS ) {
|
|
|
|
+ // 启用ssl模式;
|
|
|
|
+ dwFlags |= WINHTTP_FLAG_SECURE;
|
|
|
|
+ }
|
|
|
|
+
|
|
m_hRequest = WinHttpOpenRequest(m_hConnect,
|
|
m_hRequest = WinHttpOpenRequest(m_hConnect,
|
|
m_strMethod.c_str(),
|
|
m_strMethod.c_str(),
|
|
strObjctName.c_str(),
|
|
strObjctName.c_str(),
|
|
- NULL,
|
|
|
|
|
|
+ L"HTTP/1.1",
|
|
WINHTTP_NO_REFERER,
|
|
WINHTTP_NO_REFERER,
|
|
WINHTTP_DEFAULT_ACCEPT_TYPES,
|
|
WINHTTP_DEFAULT_ACCEPT_TYPES,
|
|
- dwOpenRequestFlag);
|
|
|
|
|
|
+ dwFlags);
|
|
|
|
|
|
if ( m_hRequest == NULL ) {
|
|
if ( m_hRequest == NULL ) {
|
|
dwError = GetLastError();
|
|
dwError = GetLastError();
|
|
@@ -106,12 +122,18 @@ BOOL HTTPHelper::_OpenHTTP()
|
|
}
|
|
}
|
|
|
|
|
|
// 如果是https,那么客户端就很容易受到无效证书的影响,现在接受任何事情都是最容易的
|
|
// 如果是https,那么客户端就很容易受到无效证书的影响,现在接受任何事情都是最容易的
|
|
- if ( m_brequireValidSSL && m_nScheme == INTERNET_SCHEME_HTTPS ) {
|
|
|
|
- DWORD dwOps = SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
|
|
|
- | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
|
|
|
- | SECURITY_FLAG_IGNORE_UNKNOWN_CA
|
|
|
|
- | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
|
|
|
|
- WinHttpSetOption(m_hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwOps, sizeof(DWORD));
|
|
|
|
|
|
+ //if ( m_brequireValidSSL && m_nScheme == INTERNET_SCHEME_HTTPS )
|
|
|
|
+ if ( m_nScheme == INTERNET_SCHEME_HTTPS )
|
|
|
|
+ {
|
|
|
|
+ DWORD dwBuffLen = sizeof(dwFlags);
|
|
|
|
+ WinHttpQueryOption(m_hRequest, WINHTTP_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
|
|
|
|
+ dwFlags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;//忽略ssl模式下的证书名称错误
|
|
|
|
+ dwFlags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
|
|
|
|
+ dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
|
|
|
|
+ dwFlags |= SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
|
|
|
|
+ WinHttpSetOption(m_hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
|
|
|
|
+ // 无证书的https;
|
|
|
|
+ //WinHttpSetOption(m_hRequest, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -146,95 +168,16 @@ BOOL HTTPHelper::_SendRequestData()
|
|
|
|
|
|
// 发送请求;
|
|
// 发送请求;
|
|
bool bSendRequestSucceed = false;
|
|
bool bSendRequestSucceed = false;
|
|
- //if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, (LPVOID)m_strPostData.c_str(), m_strPostData.size()*sizeof(wchar_t), m_strPostData.size()*sizeof(wchar_t), NULL))
|
|
|
|
- //if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, 0, _GetSendRequestSize(), NULL))
|
|
|
|
- if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL)) // 这里不设置长度,就在Header Content-Length设置;
|
|
|
|
|
|
+
|
|
|
|
+ //if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, (LPVOID)data, data_len, data_len, NULL))
|
|
|
|
+ if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, 0, _GetSendRequestSize(), NULL))
|
|
|
|
+ //if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, NULL, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL)) // 这里不设置长度,就在Header Content-Length设置;
|
|
{
|
|
{
|
|
bSendRequestSucceed = true;
|
|
bSendRequestSucceed = true;
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- // 从IE设置中查询代理信息,如果有代理,设置代理;
|
|
|
|
- WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
|
|
|
|
- memset(&proxyConfig, 0, sizeof(proxyConfig));
|
|
|
|
- if (::WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig))
|
|
|
|
- {
|
|
|
|
- if (proxyConfig.lpszAutoConfigUrl != NULL)
|
|
|
|
- {
|
|
|
|
- WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
|
|
|
|
- memset(&autoProxyOptions, 0, sizeof(autoProxyOptions));
|
|
|
|
- autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT | WINHTTP_AUTOPROXY_CONFIG_URL;
|
|
|
|
- autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP;
|
|
|
|
- autoProxyOptions.lpszAutoConfigUrl = proxyConfig.lpszAutoConfigUrl;
|
|
|
|
- autoProxyOptions.fAutoLogonIfChallenged = TRUE;
|
|
|
|
- autoProxyOptions.dwReserved = 0;
|
|
|
|
- autoProxyOptions.lpvReserved = NULL;
|
|
|
|
-
|
|
|
|
- WINHTTP_PROXY_INFO proxyInfo;
|
|
|
|
- memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|
|
|
-
|
|
|
|
- if (::WinHttpGetProxyForUrl(m_hSession, m_strUrl.c_str(), &autoProxyOptions, &proxyInfo))
|
|
|
|
- {
|
|
|
|
- if (::WinHttpSetOption(m_hRequest, WINHTTP_OPTION_PROXY, &proxyInfo, sizeof(proxyInfo)))
|
|
|
|
- {
|
|
|
|
- if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL))
|
|
|
|
- {
|
|
|
|
- bSendRequestSucceed = true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (proxyInfo.lpszProxy != NULL)
|
|
|
|
- {
|
|
|
|
- ::GlobalFree(proxyInfo.lpszProxy);
|
|
|
|
- }
|
|
|
|
- if (proxyInfo.lpszProxyBypass != NULL)
|
|
|
|
- {
|
|
|
|
- ::GlobalFree(proxyInfo.lpszProxyBypass);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- dwError = ::GetLastError();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (proxyConfig.lpszProxy != NULL)
|
|
|
|
- {
|
|
|
|
- WINHTTP_PROXY_INFO proxyInfo;
|
|
|
|
- memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|
|
|
- proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
|
|
|
|
- wchar_t szProxy[MAX_PATH] = L"";
|
|
|
|
- wcscpy_s(szProxy, MAX_PATH, proxyConfig.lpszProxy);
|
|
|
|
- proxyInfo.lpszProxy = szProxy;
|
|
|
|
-
|
|
|
|
- if (proxyConfig.lpszProxyBypass != NULL)
|
|
|
|
- {
|
|
|
|
- wchar_t szProxyBypass[MAX_PATH] = L"";
|
|
|
|
- wcscpy_s(szProxyBypass, MAX_PATH, proxyConfig.lpszProxyBypass);
|
|
|
|
- proxyInfo.lpszProxyBypass = szProxyBypass;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!::WinHttpSetOption(m_hRequest, WINHTTP_OPTION_PROXY, &proxyInfo, sizeof(proxyInfo)))
|
|
|
|
- {
|
|
|
|
- dwError = ::GetLastError();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (proxyConfig.lpszAutoConfigUrl != NULL)
|
|
|
|
- {
|
|
|
|
- ::GlobalFree(proxyConfig.lpszAutoConfigUrl);
|
|
|
|
- }
|
|
|
|
- if (proxyConfig.lpszProxy != NULL)
|
|
|
|
- {
|
|
|
|
- ::GlobalFree(proxyConfig.lpszProxy);
|
|
|
|
- }
|
|
|
|
- if (proxyConfig.lpszProxyBypass != NULL)
|
|
|
|
- {
|
|
|
|
- ::GlobalFree(proxyConfig.lpszProxyBypass);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- dwError = ::GetLastError();
|
|
|
|
- }
|
|
|
|
|
|
+ bSendRequestSucceed = _SetIEProxy();
|
|
}
|
|
}
|
|
|
|
|
|
if (bSendRequestSucceed)
|
|
if (bSendRequestSucceed)
|
|
@@ -243,7 +186,8 @@ BOOL HTTPHelper::_SendRequestData()
|
|
if ( m_strFormData.size() != 0 )
|
|
if ( m_strFormData.size() != 0 )
|
|
{
|
|
{
|
|
DWORD dwWritten = 0;
|
|
DWORD dwWritten = 0;
|
|
- if (!::WinHttpWriteData(m_hRequest, m_strFormData.c_str(), m_strFormData.size()*sizeof(wchar_t), &dwWritten))
|
|
|
|
|
|
+ // 使用WinHttpWriteData时,必须在WinHttpSendRequest中指定Data的大小;在Header中好像无效;
|
|
|
|
+ if (!::WinHttpWriteData(m_hRequest, m_strFormData.c_str(), m_strFormData.size(), &dwWritten))
|
|
{
|
|
{
|
|
dwError = ::GetLastError();
|
|
dwError = ::GetLastError();
|
|
continue;
|
|
continue;
|
|
@@ -254,7 +198,8 @@ BOOL HTTPHelper::_SendRequestData()
|
|
if ( m_strPostData.size() != 0 )
|
|
if ( m_strPostData.size() != 0 )
|
|
{
|
|
{
|
|
DWORD dwWritten = 0;
|
|
DWORD dwWritten = 0;
|
|
- if (!::WinHttpWriteData(m_hRequest, m_strPostData.c_str(), m_strPostData.size()*sizeof(wchar_t), &dwWritten))
|
|
|
|
|
|
+ // 使用WinHttpWriteData时,必须在WinHttpSendRequest中指定Data的大小;在Header中好像无效;
|
|
|
|
+ if (!::WinHttpWriteData(m_hRequest, m_strPostData.c_str(), m_strPostData.size(), &dwWritten))
|
|
{
|
|
{
|
|
dwError = ::GetLastError();
|
|
dwError = ::GetLastError();
|
|
continue;
|
|
continue;
|
|
@@ -306,6 +251,7 @@ BOOL HTTPHelper::_ReceiveResponse(std::wstring &strHeaer, DWORD &dwStatusCode, s
|
|
}
|
|
}
|
|
|
|
|
|
dwSize = 0;
|
|
dwSize = 0;
|
|
|
|
+ // 获取返回状态大小;
|
|
bResult = ::WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
|
|
bResult = ::WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
|
|
if (bResult || (!bResult && (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)))
|
|
if (bResult || (!bResult && (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)))
|
|
{
|
|
{
|
|
@@ -316,12 +262,10 @@ BOOL HTTPHelper::_ReceiveResponse(std::wstring &strHeaer, DWORD &dwStatusCode, s
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if ( dwStatusCode != 200 )
|
|
|
|
- return FALSE;
|
|
|
|
-
|
|
|
|
do
|
|
do
|
|
{
|
|
{
|
|
dwSize = 0;
|
|
dwSize = 0;
|
|
|
|
+ // 查询是否有可用数据;
|
|
if (WinHttpQueryDataAvailable(m_hRequest, &dwSize))
|
|
if (WinHttpQueryDataAvailable(m_hRequest, &dwSize))
|
|
{
|
|
{
|
|
if (dwSize==0)
|
|
if (dwSize==0)
|
|
@@ -348,7 +292,7 @@ BOOL HTTPHelper::_ReceiveResponse(std::wstring &strHeaer, DWORD &dwStatusCode, s
|
|
return TRUE;
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
|
|
-bool HTTPHelper::SendRequest(HTTPITEM &item,HTTPRESULT &httpResult)
|
|
|
|
|
|
+bool HTTPHelper::SendRequest(HTTPRESULT &httpResult)
|
|
{
|
|
{
|
|
if (!_OpenHTTP())
|
|
if (!_OpenHTTP())
|
|
return false;
|
|
return false;
|
|
@@ -386,9 +330,9 @@ BOOL HTTPHelper::_SetRequestHeaders()
|
|
return FALSE;
|
|
return FALSE;
|
|
|
|
|
|
if ( m_strHeader.size() > 0 ) {
|
|
if ( m_strHeader.size() > 0 ) {
|
|
-#if 1 // WinHttpSendRequest里不设置数据长度,这里必须设置;
|
|
|
|
|
|
+#if 0 // WinHttpSendRequest里不设置数据长度,这里必须设置;
|
|
wchar_t szContentLength[MAX_PATH] = {0};
|
|
wchar_t szContentLength[MAX_PATH] = {0};
|
|
- swprintf_s(szContentLength, L"%s%d\r\n", CONTENT_LENGTH, _GetSendRequestSize());
|
|
|
|
|
|
+ swprintf_s(szContentLength, L"%s: %d\r\n", CONTENT_LENGTH, _GetSendRequestSize());
|
|
m_strHeader.append(szContentLength);
|
|
m_strHeader.append(szContentLength);
|
|
#endif
|
|
#endif
|
|
if ( !WinHttpAddRequestHeaders(m_hRequest, m_strHeader.c_str(), m_strHeader.size(), WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON) ) {
|
|
if ( !WinHttpAddRequestHeaders(m_hRequest, m_strHeader.c_str(), m_strHeader.size(), WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON) ) {
|
|
@@ -402,7 +346,7 @@ BOOL HTTPHelper::_SetRequestHeaders()
|
|
|
|
|
|
void HTTPHelper::_SetContentLength()
|
|
void HTTPHelper::_SetContentLength()
|
|
{
|
|
{
|
|
- int nPos = m_strHeader.find(CONTENT_LENGTH);
|
|
|
|
|
|
+ int nPos = m_strHeader.find(L"Content-Length: ");
|
|
if ( nPos != std::wstring::npos) {
|
|
if ( nPos != std::wstring::npos) {
|
|
// 删除原有的请求头;
|
|
// 删除原有的请求头;
|
|
int nPos2 = m_strHeader.find(L"\r\n",nPos);
|
|
int nPos2 = m_strHeader.find(L"\r\n",nPos);
|
|
@@ -413,7 +357,7 @@ void HTTPHelper::_SetContentLength()
|
|
}
|
|
}
|
|
|
|
|
|
wchar_t szContentLenght[MAX_PATH] = {0};
|
|
wchar_t szContentLenght[MAX_PATH] = {0};
|
|
- swprintf_s(szContentLenght, L"%s%d", CONTENT_LENGTH, m_strFormData.size());
|
|
|
|
|
|
+ swprintf_s(szContentLenght, L"%s: %d", L"Content-Length", m_strFormData.size());
|
|
|
|
|
|
// 添加Form表单头;
|
|
// 添加Form表单头;
|
|
m_strHeader.append(szContentLenght);
|
|
m_strHeader.append(szContentLenght);
|
|
@@ -424,9 +368,9 @@ DWORD HTTPHelper::_GetSendRequestSize()
|
|
{
|
|
{
|
|
DWORD dwSize(0);
|
|
DWORD dwSize(0);
|
|
if ( m_strPostData.size())
|
|
if ( m_strPostData.size())
|
|
- dwSize = m_strPostData.size()*sizeof(wchar_t);
|
|
|
|
|
|
+ dwSize = m_strPostData.size();
|
|
else if ( m_strFormData.size())
|
|
else if ( m_strFormData.size())
|
|
- dwSize = m_strFormData.size()*sizeof(wchar_t);
|
|
|
|
|
|
+ dwSize = m_strFormData.size();
|
|
|
|
|
|
return dwSize;
|
|
return dwSize;
|
|
}
|
|
}
|
|
@@ -486,6 +430,95 @@ BOOL HTTPHelper::_SetProxy()
|
|
return TRUE;
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+BOOL HTTPHelper::_SetIEProxy()
|
|
|
|
+{
|
|
|
|
+ // 从IE设置中查询代理信息,如果有代理,设置代理;
|
|
|
|
+ DWORD dwError(0);
|
|
|
|
+ BOOL bSendRequestSucceed=FALSE;
|
|
|
|
+ WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
|
|
|
|
+ memset(&proxyConfig, 0, sizeof(proxyConfig));
|
|
|
|
+ if (::WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig))
|
|
|
|
+ {
|
|
|
|
+ if (proxyConfig.lpszAutoConfigUrl != NULL)
|
|
|
|
+ {
|
|
|
|
+ WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
|
|
|
|
+ memset(&autoProxyOptions, 0, sizeof(autoProxyOptions));
|
|
|
|
+ autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT | WINHTTP_AUTOPROXY_CONFIG_URL;
|
|
|
|
+ autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP;
|
|
|
|
+ autoProxyOptions.lpszAutoConfigUrl = proxyConfig.lpszAutoConfigUrl;
|
|
|
|
+ autoProxyOptions.fAutoLogonIfChallenged = TRUE;
|
|
|
|
+ autoProxyOptions.dwReserved = 0;
|
|
|
|
+ autoProxyOptions.lpvReserved = NULL;
|
|
|
|
+
|
|
|
|
+ WINHTTP_PROXY_INFO proxyInfo;
|
|
|
|
+ memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|
|
|
+
|
|
|
|
+ if (::WinHttpGetProxyForUrl(m_hSession, m_strUrl.c_str(), &autoProxyOptions, &proxyInfo))
|
|
|
|
+ {
|
|
|
|
+ if (::WinHttpSetOption(m_hRequest, WINHTTP_OPTION_PROXY, &proxyInfo, sizeof(proxyInfo)))
|
|
|
|
+ {
|
|
|
|
+ if (::WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL))
|
|
|
|
+ {
|
|
|
|
+ bSendRequestSucceed = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (proxyInfo.lpszProxy != NULL)
|
|
|
|
+ {
|
|
|
|
+ ::GlobalFree(proxyInfo.lpszProxy);
|
|
|
|
+ }
|
|
|
|
+ if (proxyInfo.lpszProxyBypass != NULL)
|
|
|
|
+ {
|
|
|
|
+ ::GlobalFree(proxyInfo.lpszProxyBypass);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ dwError = ::GetLastError();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else if (proxyConfig.lpszProxy != NULL)
|
|
|
|
+ {
|
|
|
|
+ WINHTTP_PROXY_INFO proxyInfo;
|
|
|
|
+ memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|
|
|
+ proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
|
|
|
|
+ wchar_t szProxy[MAX_PATH] = L"";
|
|
|
|
+ wcscpy_s(szProxy, MAX_PATH, proxyConfig.lpszProxy);
|
|
|
|
+ proxyInfo.lpszProxy = szProxy;
|
|
|
|
+
|
|
|
|
+ if (proxyConfig.lpszProxyBypass != NULL)
|
|
|
|
+ {
|
|
|
|
+ wchar_t szProxyBypass[MAX_PATH] = L"";
|
|
|
|
+ wcscpy_s(szProxyBypass, MAX_PATH, proxyConfig.lpszProxyBypass);
|
|
|
|
+ proxyInfo.lpszProxyBypass = szProxyBypass;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!::WinHttpSetOption(m_hRequest, WINHTTP_OPTION_PROXY, &proxyInfo, sizeof(proxyInfo)))
|
|
|
|
+ {
|
|
|
|
+ dwError = ::GetLastError();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (proxyConfig.lpszAutoConfigUrl != NULL)
|
|
|
|
+ {
|
|
|
|
+ ::GlobalFree(proxyConfig.lpszAutoConfigUrl);
|
|
|
|
+ }
|
|
|
|
+ if (proxyConfig.lpszProxy != NULL)
|
|
|
|
+ {
|
|
|
|
+ ::GlobalFree(proxyConfig.lpszProxy);
|
|
|
|
+ }
|
|
|
|
+ if (proxyConfig.lpszProxyBypass != NULL)
|
|
|
|
+ {
|
|
|
|
+ ::GlobalFree(proxyConfig.lpszProxyBypass);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ dwError = ::GetLastError();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return bSendRequestSucceed;
|
|
|
|
+}
|
|
|
|
+
|
|
BOOL HTTPHelper::_SetAutoRedirect()
|
|
BOOL HTTPHelper::_SetAutoRedirect()
|
|
{
|
|
{
|
|
if ( !m_bAllowAutoRedirect ) {
|
|
if ( !m_bAllowAutoRedirect ) {
|
|
@@ -499,22 +532,6 @@ BOOL HTTPHelper::_SetAutoRedirect()
|
|
return TRUE;
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
|
|
-void HTTPHelper::SetContentType(std::wstring strContentType)
|
|
|
|
-{
|
|
|
|
- int nPos = m_strHeader.find(CONTENT_TYPE);
|
|
|
|
- if ( nPos != std::wstring::npos) {
|
|
|
|
- // 删除原有的请求头;
|
|
|
|
- int nPos2 = m_strHeader.find(L"\r\n",nPos);
|
|
|
|
- if ( nPos2 !=std::wstring::npos )
|
|
|
|
- m_strHeader.erase(nPos, nPos2-nPos+2);
|
|
|
|
- else
|
|
|
|
- m_strHeader.erase(nPos);
|
|
|
|
- }
|
|
|
|
- // 添加Form表单头;
|
|
|
|
- m_strHeader.append(strContentType);
|
|
|
|
- m_strHeader.append(CRLF);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
void HTTPHelper::AddQueryParams(std::wstring strName, std::wstring strValue)
|
|
void HTTPHelper::AddQueryParams(std::wstring strName, std::wstring strValue)
|
|
{
|
|
{
|
|
if ( m_strQueryParams.size() )
|
|
if ( m_strQueryParams.size() )
|
|
@@ -567,93 +584,90 @@ void HTTPHelper::AddFormUrlEncodedDataHeader()
|
|
m_strFormData.clear();
|
|
m_strFormData.clear();
|
|
}
|
|
}
|
|
|
|
|
|
-void HTTPHelper::AddBodyFormDataText(std::wstring strName, std::wstring strValue)
|
|
|
|
|
|
+void HTTPHelper::AddBodyFormDataText(std::string strName, std::string strValue)
|
|
{
|
|
{
|
|
AddFormDataHeader();//放到外头,只调用一次就好;
|
|
AddFormDataHeader();//放到外头,只调用一次就好;
|
|
- int nSize = wcslen(FORMDATA_TEXT_FORMAT) + strName.size() + strValue.size();
|
|
|
|
|
|
+ int nSize = strlen(FORMDATA_TEXT_FORMAT) + strName.size() + strValue.size();
|
|
if ( nSize < K0 ) {
|
|
if ( nSize < K0 ) {
|
|
- wchar_t szFormData[K0] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
|
|
|
|
+ char szFormData[K0] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K1 ) {
|
|
else if ( nSize < K1 ) {
|
|
- wchar_t szFormData[K1] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
|
|
|
|
+ char szFormData[K1] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K2 ) {
|
|
else if ( nSize < K2 ) {
|
|
- wchar_t szFormData[K2] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
|
|
|
|
+ char szFormData[K2] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K4 ) {
|
|
else if ( nSize < K4 ) {
|
|
- wchar_t szFormData[K4] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
|
|
|
|
+ char szFormData[K4] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K8 ) {
|
|
else if ( nSize < K8 ) {
|
|
- wchar_t szFormData[K8] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
|
|
|
|
+ char szFormData[K8] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_TEXT_FORMAT, strName.c_str(), strName.c_str(), strValue.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
} else {
|
|
} else {
|
|
-
|
|
|
|
|
|
+ // new
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void HTTPHelper::AddBodyFormDataFile(std::wstring strName, std::wstring strFilePath, std::wstring strContentType)
|
|
|
|
|
|
+void HTTPHelper::AddBodyFormDataFile(std::string strName, std::string strFilePath, std::wstring strContentType)
|
|
{
|
|
{
|
|
AddFormDataHeader();//放到外头,只调用一次就好;
|
|
AddFormDataHeader();//放到外头,只调用一次就好;
|
|
- int nSize = wcslen(FORMDATA_FILE_FORMAT) + strName.size() + strFilePath.size() + strContentType.size();
|
|
|
|
|
|
+ int nSize = strlen(FORMDATA_FILE_FORMAT) + strName.size() + strFilePath.size() + strContentType.size();
|
|
if ( nSize < K0 ) {
|
|
if ( nSize < K0 ) {
|
|
- wchar_t szFormData[K0] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
|
|
|
|
+ char szFormData[K0] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K1 ) {
|
|
else if ( nSize < K1 ) {
|
|
- wchar_t szFormData[K1] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
|
|
|
|
+ char szFormData[K1] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K2 ) {
|
|
else if ( nSize < K2 ) {
|
|
- wchar_t szFormData[K2] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
|
|
|
|
+ char szFormData[K2] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K4 ) {
|
|
else if ( nSize < K4 ) {
|
|
- wchar_t szFormData[K4] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
|
|
|
|
+ char szFormData[K4] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
}
|
|
}
|
|
else if ( nSize < K8 ) {
|
|
else if ( nSize < K8 ) {
|
|
- wchar_t szFormData[K8] = {0};
|
|
|
|
- swprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
|
|
|
|
+ char szFormData[K8] = {0};
|
|
|
|
+ sprintf_s(szFormData, FORMDATA_FILE_FORMAT, strName.c_str(), strName.c_str(), strFilePath.c_str(), strContentType.c_str());
|
|
m_strFormData.append(szFormData);
|
|
m_strFormData.append(szFormData);
|
|
} else {
|
|
} else {
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void HTTPHelper::AddBodyFormUrlEncodedData(std::wstring strName, std::wstring strValue)
|
|
|
|
|
|
+void HTTPHelper::AddBodyFormUrlEncodedData(std::string strName, std::string strValue)
|
|
{
|
|
{
|
|
AddFormUrlEncodedDataHeader();//放到外头,只调用一次就好;
|
|
AddFormUrlEncodedDataHeader();//放到外头,只调用一次就好;
|
|
if ( m_strFormData.size() )
|
|
if ( m_strFormData.size() )
|
|
- m_strFormData.append(L"&");
|
|
|
|
|
|
+ m_strFormData.append("&");
|
|
|
|
|
|
m_strFormData.append(strName);
|
|
m_strFormData.append(strName);
|
|
- m_strFormData.append(L"=");
|
|
|
|
|
|
+ m_strFormData.append("=");
|
|
m_strFormData.append(strValue);
|
|
m_strFormData.append(strValue);
|
|
// 重新计算长度;
|
|
// 重新计算长度;
|
|
_SetContentLength();
|
|
_SetContentLength();
|
|
}
|
|
}
|
|
|
|
|
|
-void HTTPHelper::AddBodyRawData(std::wstring strRawData, std::wstring strContentType/* = */)
|
|
|
|
|
|
+void HTTPHelper::AddBodyRawData(std::string strRawData, std::wstring strContentType/* = */)
|
|
{
|
|
{
|
|
m_strPostData = strRawData;
|
|
m_strPostData = strRawData;
|
|
// 代替ContentType
|
|
// 代替ContentType
|
|
- strRawData = CONTENT_TYPE;
|
|
|
|
- strRawData.append(strContentType);
|
|
|
|
- strRawData.append(CRLF);
|
|
|
|
- SetContentType(strRawData);
|
|
|
|
|
|
+ AddRequestHeaders(CONTENT_TYPE, strContentType);
|
|
}
|
|
}
|
|
|
|
|
|
void HTTPHelper::AddBodyBinary(std::wstring strFilePath)
|
|
void HTTPHelper::AddBodyBinary(std::wstring strFilePath)
|