void vmsUrlMonRequestCollector::onInternetProtocolSink_ReportResult(IInternetProtocolSink *pSink, HRESULT hrResult, DWORD dwError, LPCWSTR szResult) { vmsCriticalSectionAutoLock csal (&m_csReq); int nIndex = findRequestIndexBySink (pSink); ATLASSERT (nIndex != -1); if (nIndex == -1) return; Request *req = getRequest (nIndex); CloseRequest (req); }
void vmsUrlMonRequestCollector::CloseTimedoutRequests(void) { vmsCriticalSectionAutoLock csal (&m_csReq); DWORD dwTicks = GetTickCount (); for (size_t i = 0; i < m_vReq.size (); i++) { if ((m_vReq [i]->dwState & Request::GotResponse) || m_vReq [i]->dwTicksCompleted) continue; if (dwTicks - m_vReq [i]->dwTicksStarted >= 2*60*1000) CloseRequest (m_vReq [i]); } }
void vmsUrlMonRequestCollector::RemoveTooOldRequests(void) { vmsCriticalSectionAutoLock csal (&m_csReq); size_t cOld = 0; DWORD dwTicks = GetTickCount (); for (size_t i = 0; i < m_vReq.size (); i++, cOld++) { if (m_vReq [i]->dwTicksCompleted && dwTicks - m_vReq [i]->dwTicksCompleted < 5*60*1000) break; if (dwTicks - m_vReq [i]->dwTicksStarted < 15*60*1000) break; if (!m_vReq [i]->dwTicksCompleted) CloseRequest (m_vReq [i]); } if (cOld) m_vReq.erase (m_vReq.begin (), m_vReq.begin () + cOld); }
// 打开HTTP请求函数 BOOL CHttpClient::OpenRequest(LPCTSTR lpszUrl, HTTP_REQ_METHOD nReqMethod/* = REQ_METHOD_GET*/) { TCHAR szScheme[INTERNET_MAX_URL_LENGTH] = {0}; TCHAR szHostName[INTERNET_MAX_URL_LENGTH] = {0}; TCHAR szUrlPath[INTERNET_MAX_URL_LENGTH] = {0}; WORD nPort = 0; DWORD dwAccessType; LPCTSTR lpszProxy; BOOL bRet; bRet = ParseURL(lpszUrl, szScheme, INTERNET_MAX_URL_LENGTH, // 解析Url szHostName, INTERNET_MAX_URL_LENGTH, nPort, szUrlPath, INTERNET_MAX_URL_LENGTH); if (!bRet) return FALSE; m_hCompleteEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL); // 创建事件句柄 m_hCancelEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL); if (NULL == m_hCompleteEvent || NULL == m_hCancelEvent) { CloseRequest(); return FALSE; } if (m_strProxy.size() > 0) { dwAccessType = INTERNET_OPEN_TYPE_PROXY; lpszProxy = m_strProxy.c_str(); } else { dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG; lpszProxy = NULL; } m_hInternet = ::InternetOpen(CHROME_USER_AGENT, dwAccessType, lpszProxy, NULL, INTERNET_FLAG_ASYNC); if (NULL == m_hInternet) { CloseRequest(); return FALSE; } if (m_strUser.size() > 0) // 设置代理用户名 { ::InternetSetOptionEx(m_hInternet, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)m_strUser.c_str(), m_strUser.size() + 1, 0); } if (m_strPwd.size() > 0) // 设置代理密码 { ::InternetSetOptionEx(m_hInternet, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)m_strPwd.c_str(), m_strPwd.size() + 1, 0); } // DWORD dwTimeOut; // DWORD dwSize = sizeof(dwTimeOut); // ::InternetQueryOption(m_hInternet, INTERNET_OPTION_CONNECT_TIMEOUT, (LPVOID)&dwTimeOut, &dwSize); INTERNET_STATUS_CALLBACK lpCallBackFunc; // 设置状态回调函数 lpCallBackFunc = ::InternetSetStatusCallback(m_hInternet, (INTERNET_STATUS_CALLBACK)&StatusCallback); if (INTERNET_INVALID_STATUS_CALLBACK == lpCallBackFunc) { CloseRequest(); return FALSE; } m_dwContext = CONNECTED_EVENT; m_hConnect = ::InternetConnect(m_hInternet, szHostName, nPort, NULL, _T("HTTP/1.1"), INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)this); if (NULL == m_hConnect) { if (::GetLastError() != ERROR_IO_PENDING) { CloseRequest(); return FALSE; } } bRet = WaitForEvent(CONNECTED_EVENT, m_dwConnectTimeOut); if (!bRet) { CloseRequest(); return FALSE; } TCHAR * lpMethod; if (nReqMethod == REQ_METHOD_GET) lpMethod = _T("GET"); else lpMethod = _T("POST"); DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE; if (INTERNET_DEFAULT_HTTPS_PORT == nPort) dwFlags |= INTERNET_FLAG_SECURE; m_dwContext = REQUEST_OPENED_EVENT; m_hRequest = ::HttpOpenRequest(m_hConnect, lpMethod, szUrlPath, _T("HTTP/1.1"), NULL, NULL, dwFlags, (DWORD_PTR)this); if (NULL == m_hRequest) { if (::GetLastError() != ERROR_IO_PENDING) { CloseRequest(); return FALSE; } } bRet = WaitForEvent(REQUEST_OPENED_EVENT, INFINITE); if (!bRet) { CloseRequest(); return FALSE; } return TRUE; }
CHttpClient::~CHttpClient(void) { CloseRequest(); }