BOOL CProxyIP::GetFirstGoalUrl(const TCHAR* pSrcUrl, TCHAR* pGoalUrl) { BOOL bRet = FALSE; CHttpFile* pHttpFile = (CHttpFile *)m_pSession->OpenURL(pSrcUrl); DWORD dwStatusCode = 0; pHttpFile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { CString strData; wstring subStr = _T("国内网页http代理ip地址服务器"); while(pHttpFile->ReadString(strData)) { TCHAR mainStr[MAX_STR_LEN] = {0}; ConvertMultiByteToWideChar((char *)strData.GetBuffer(), mainStr, MAX_STR_LEN); wstring wMainStr = mainStr; if (wMainStr.find(subStr) < wMainStr.length()) { int startLoc = wMainStr.find(_T("href")); int endLoc = wMainStr.find(_T("title")); endLoc -= 2; startLoc += 6; wstring wTempUrl = wMainStr.substr(startLoc, endLoc-startLoc).c_str(); wmemcpy(pGoalUrl, wTempUrl.c_str(), wcslen(wTempUrl.c_str())+1); bRet = TRUE; break; } } } pHttpFile->Close(); delete pHttpFile; return bRet; }
std::string urlGet(const char* pUrl) { CInternetSession session(__argv[0]); CHttpFile *file = NULL; CString strHtml = ""; //存放网页数据 char* headers = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)" ; try { file = (CHttpFile*)session.OpenURL(pUrl , 1 , INTERNET_FLAG_TRANSFER_ASCII , headers , strlen(headers)) ; } catch(CInternetException * m_pException){ file = NULL; m_pException->Delete(); session.Close(); return "" ; } CString strLine; if(file != NULL) { while(file->ReadString(strLine) != NULL) { strHtml += strLine; //strHtml +="\n" ; } file->Close(); delete file; file = NULL; } session.Close(); return std::string(strHtml) ; }
void C51JobWebPost::TestProxy() { CInternetSession session; CHttpFile *file = NULL; INTERNET_PROXY_INFO proxyinfo; proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY; proxyinfo.lpszProxy ="122.205.95.14:80"; proxyinfo.lpszProxyBypass = NULL; session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO)); try{ file = (CHttpFile*)session.OpenURL("http://www.ip138.com/ip2city.asp",1, INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE); }catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); AfxMessageBox("CInternetException"); return; } CString strLine; CString strResult = ""; if(file != NULL){ while(file->ReadString(strLine) != NULL){ strResult += strLine; } }else{ AfxMessageBox("fail"); } file->Close(); session.Close(); }
void CHttpPostDlg::OnBtnSendpost() { CInternetSession m_InetSession(_T("session"), 0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_DONT_CACHE); //设置不缓冲 CHttpConnection* pServer = NULL; CHttpFile* pFile = NULL; CString strHtml = ""; CString ActionServer = _T("www.cqjg.gov.cn"); CString strRequest = _T("LicenseTxt=AG8091&VIN=LJDAAA21560205432"); //POST过去的数据 CString strHeaders = "Accept: text*/*\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon;"; int nRead = 0; try { INTERNET_PORT nPort = 80; //端口 pServer = m_InetSession.GetHttpConnection(ActionServer, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/netcar/FindOne.aspx"); pFile->AddRequestHeaders(strHeaders); pFile->SendRequestEx(strRequest.GetLength()); pFile->WriteString(strRequest); //重要-->m_Request 中有"name=aaa&name2=BBB&..." pFile->EndRequest(); DWORD dwRet; pFile->QueryInfoStatusCode(dwRet); if (dwRet == HTTP_STATUS_OK) { CString strLine; while ((nRead = pFile->ReadString(strLine))>0) { strHtml += strLine + "\n";; } } int pos = strHtml.Find(_T("<li class=\"lithreeC\">")); if(pos != -1) { CString Value = strHtml.Mid(pos,500); CFile file("test.html",CFile::modeCreate|CFile::modeWrite); file.WriteHuge(Value.GetBuffer(0),Value.GetLength()); file.Close(); //MessageBox(Value); } delete pFile; delete pServer; } catch (CInternetException* e) { char strErrorBuf[255]; e->GetErrorMessage(strErrorBuf,255,NULL); AfxMessageBox(strErrorBuf,MB_ICONINFORMATION); } // SendPost(); }
CString CUpdateApp::GetWebStieHtml(CString strUrl) { CInternetSession mySession(NULL,0); CHttpFile* myHttpFile = NULL; myHttpFile = (CHttpFile*)mySession.OpenURL(strUrl);//str是要打开的地址 CString myData; CString csHtmlContent; while(myHttpFile->ReadString(myData)) { csHtmlContent += myData; } return csHtmlContent; }
DWORD CLoginDlg::userProfile(LPCTSTR serverURL, LPCTSTR requestPage) { #ifdef _HTTPS CInternetSession session(_T("HelloChat"), INTERNET_FLAG_SECURE); CHttpConnection* pConnection = session.GetHttpConnection(serverURL, INTERNET_SERVICE_HTTP, INTERNET_DEFAULT_HTTPS_PORT); CString strToken = L"token:"; strToken = strToken + m_strMyToken + L"\r\n"; CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, requestPage, NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_COOKIES); #else CInternetSession session(_T("HelloChat"), PRE_CONFIG_INTERNET_ACCESS); CHttpConnection* pConnection = session.GetHttpConnection(serverURL); CString strToken = L"token:"; //strToken = strToken + m_strToken + L"\r\n"; strToken = strToken + m_strMyToken + L"\r\n"; CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, requestPage, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_COOKIES); #endif VERIFY(pFile->AddRequestHeaders(HEADER)); VERIFY(pFile->AddRequestHeaders(strToken)); VERIFY(pFile->SendRequest()); // GET POST STATUS DWORD dwPostStatus = 0; VERIFY(pFile->QueryInfoStatusCode(dwPostStatus)); CString strBuffer = L""; BOOL brtn = pFile->ReadString(strBuffer); char* rtnBuffer = LPSTR(LPCTSTR(strBuffer)); if (dwPostStatus == HTTP_STATUS_OK) { BOOL bRtn = dataParser(rtnBuffer); if (!bRtn){ AfxMessageBox(L"User Info Paser Error"); } } else{ CComm func; func.WriteErrorLog(rtnBuffer); } pFile->Close(); return dwPostStatus; }
BOOL CProxyIP::FillGoalUrlVec(const TCHAR* pFirstGoalUrl) { BOOL bRet = FALSE; int totalPage = 0; CHttpFile* pHttpFile = (CHttpFile *)m_pSession->OpenURL(pFirstGoalUrl); DWORD dwStatusCode = 0; pHttpFile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { BOOL bCheck = FALSE; CString strData; wstring subStrPage = _T("class=\"pagelist\""); while(pHttpFile->ReadString(strData)) { TCHAR mainStr[MAX_STR_LEN] = {0}; ConvertMultiByteToWideChar((char *)strData.GetBuffer(), mainStr, MAX_STR_LEN); wstring wMainStr = mainStr; if (bCheck) { int lenn = wMainStr.find(_T("共")); wstring wsPage = wMainStr.substr(lenn+1, 1).c_str(); totalPage = _wtoi(wsPage.c_str()); bCheck = FALSE; break; } if (wMainStr.find(subStrPage) < wMainStr.length()) { bCheck = TRUE; } } wstring strGoalUrl = pFirstGoalUrl; int lenHtml = strGoalUrl.find(_T(".html")); wstring newStrGoalUrl = strGoalUrl.substr(0, lenHtml); for (int j = 2; j <= totalPage; j++) { TCHAR tChar[MAX_URL_LEN] = {0}; swprintf(tChar, MAX_URL_LEN, _T("%s_%d.html"), newStrGoalUrl.c_str(), j); m_goalUrlVec.push_back(tChar); } bRet = TRUE; } pHttpFile->Close(); delete pHttpFile; return bRet; }
/* =================================================================== * GET Tally TB Response for a given request * =================================================================== */ BOOL CTallyExporterDlg::GetTallyTBResponse(CString& xmlTBRequestRef,vector<string>& allRowsOfTBRef, int port) { CInternetSession session(_T("TB Session")); CHttpConnection* pServer = NULL; CHttpFile* pFile = NULL; try { CString strServerName; DWORD dwRet = 0; CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded;Accept-Encoding: gzip,deflate"); CString acceptedTypes[] = {_T("text/html")}; pServer = session.GetHttpConnection(_T("localhost"),NULL, port, NULL, NULL); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, _T(""), NULL,1,(LPCTSTR*)acceptedTypes,NULL,INTERNET_FLAG_EXISTING_CONNECT); pFile->SendRequest(strHeaders, (LPVOID)(LPCTSTR)xmlTBRequestRef, xmlTBRequestRef.GetLength()); pFile->QueryInfoStatusCode(dwRet); CString csvLinesToBeRead; CString& csvLinesToBeReadRef = csvLinesToBeRead; if (dwRet == HTTP_STATUS_OK) { while (pFile->ReadString(csvLinesToBeReadRef) == TRUE) { allRowsOfTBRef.push_back((LPCTSTR)csvLinesToBeReadRef); } } else { //Do something as server is not sending response as expected. } delete pFile; delete pServer; } catch (CInternetException* pEx) { //catch errors from WinInet TCHAR pszError[64]; pEx->GetErrorMessage(pszError, 64); _tprintf_s(_T("%63s"), pszError); } session.Close(); return false; }
bool CWebpageHandler::GetSourceHtml(CString theUrl,CString fileName) { CInternetSession session; CHttpFile *file = NULL; CString strURL = theUrl; CString strHtml = _T(""); //存放网页数据 try{ file = (CHttpFile*)session.OpenURL(strURL); }catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); MessageBox(_T("网络连接失败!")); return false; } CString strLine; if(file != NULL){ while(file->ReadString(strLine) != NULL){ strHtml += strLine; } }else{ MessageBox(_T("读取网络数据失败!")); return false; } CFile file0(fileName, CFile::modeCreate|CFile::modeWrite); int len = strHtml.GetLength()*2; file0.Write(strHtml, len); session.Close(); file->Close(); file0.Close(); delete file; file = NULL; m_htmlStr = strHtml; m_url = theUrl; return true; }
BOOL CProxyIP::FillIpVec() { int i = 0; for (i = 0; i < m_goalUrlVec.size(); i++) { CHttpFile* pHttpFile = (CHttpFile *)m_pSession->OpenURL(m_goalUrlVec[i].c_str()); DWORD dwStatusCode = 0; pHttpFile->QueryInfoStatusCode(dwStatusCode); if (dwStatusCode == HTTP_STATUS_OK) { BOOL bCheck = FALSE; CString strData; wstring subStr = _T("class=\"cont_ad\""); while(pHttpFile->ReadString(strData)) { TCHAR mainStr[MAX_STR_LEN] = {0}; ConvertMultiByteToWideChar((char *)strData.GetBuffer(), mainStr, MAX_STR_LEN); wstring wMainStr = mainStr; if (bCheck) { if (wMainStr.find(_T("</span>")) < wMainStr.length()) { break; } TCHAR firstPosChar = wMainStr.at(0); if (firstPosChar < 48 || firstPosChar > 57) { continue; } m_IpVec.push_back(wMainStr); m_totalIp++; } if (wMainStr.find(subStr) < wMainStr.length()) { bCheck = TRUE; } } } pHttpFile->Close(); delete pHttpFile; } return TRUE; }
CString StockRetriever::GetStockInfo(CString url) { CInternetSession inter_session; CString strHtml; CHttpFile* file =NULL; try{ file = (CHttpFile*)inter_session.OpenURL(url); }catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); inter_session.Close(); return ""; //AfxMessageBox("CInternetException"); } CString strLine; if(file != NULL){ while(file->ReadString(strLine) != NULL){ strHtml += strLine; } }else{ return ""; //AfxMessageBox("fail"); } inter_session.Close(); file->Close(); delete file; file = NULL; int start = strHtml.Find('"'); int end = strHtml.Find('"', start+1); //ASSERT( end > start+1 ); if ( ! (end > start+1 && start >0 )) return ""; CString ret = strHtml.Mid(start+1, end-start-1); return ret; }
AINIKU_API CString getUrl2(CString url) { if (url == "") { return ""; } CInternetSession mySession((LPCTSTR)"aaa", 0);//如果不指定aaa(随便的一个字符串)的话debug模式下会报错 CHttpFile* htmlFile = NULL; CString str, strHtml; TRY{ htmlFile = (CHttpFile*)mySession.OpenURL(url);//打开连接 while (htmlFile->ReadString(str)) { char *pStr = (char*)str.GetBuffer(str.GetLength()); //取得str对象的原始字符串 int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, pStr, -1, NULL, 0); //取得所需缓存的多少 wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间 MultiByteToWideChar(CP_UTF8, 0, pStr, -1, pBuffer, nBufferSize*sizeof(wchar_t));//转码 strHtml += pBuffer; free(pBuffer); //释放缓存 } return strHtml; }CATCH(CException, e){ TCHAR err[1024]; e->GetErrorMessage(err, 1024); return ""; }
string GetHtmlSource(CString strURL) { CInternetSession session; CHttpFile *file = NULL; CString strHtml = _T(""); //存放网页数据 file =(CHttpFile*)session.OpenURL(strURL,1,INTERNET_FLAG_RELOAD); //CString strLine; char sRecived[1024]; if(file != NULL) { while(file->ReadString((LPTSTR)sRecived,1024)!=NULL) { strHtml += sRecived; } } session.Close(); if(file!=NULL) file->Close(); delete file; file = NULL; string html = CT2A(strHtml.GetBuffer(0)); return html; }
int main(int argc, char* argv[]) { ShowBanner(); if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { cerr << _T("MFC Failed to initialize.\n"); return 1; } if (argc < 2 || !ParseOptions(argc, argv) || pszURL == NULL) ShowUsage(); int nRetCode = 0; CTearSession session(_T("TEAR - MFC Sample App"), dwAccessType); CHttpConnection* pServer = NULL; CHttpFile* pFile = NULL; try { // check to see if this is a reasonable URL CString strServerName; CString strObject; INTERNET_PORT nPort; DWORD dwServiceType; if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) || dwServiceType != INTERNET_SERVICE_HTTP) { cerr << _T("Error: can only use URLs beginning with http://") << endl; ThrowTearException(1); } if (bProgressMode) { cerr << _T("Opening Internet..."); VERIFY(session.EnableStatusCallback(TRUE)); } pServer = session.GetHttpConnection(strServerName, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags); pFile->AddRequestHeaders(szHeaders); pFile->SendRequest(); DWORD dwRet; pFile->QueryInfoStatusCode(dwRet); // if access was denied, prompt the user for the password if (dwRet == HTTP_STATUS_DENIED) { DWORD dwPrompt; dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD, FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL); // if the user cancelled the dialog, bail out if (dwPrompt != ERROR_INTERNET_FORCE_RETRY) { cerr << _T("Access denied: Invalid password\n"); ThrowTearException(1); } pFile->SendRequest(); pFile->QueryInfoStatusCode(dwRet); } CString strNewLocation; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); // were we redirected? // these response status codes come from WININET.H if (dwRet == HTTP_STATUS_MOVED || dwRet == HTTP_STATUS_REDIRECT || dwRet == HTTP_STATUS_REDIRECT_METHOD) { CString strNewLocation; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); int nPlace = strNewLocation.Find(_T("Location: ")); if (nPlace == -1) { cerr << _T("Error: Site redirects with no new location") << endl; ThrowTearException(2); } strNewLocation = strNewLocation.Mid(nPlace + 10); nPlace = strNewLocation.Find('\n'); if (nPlace > 0) strNewLocation = strNewLocation.Left(nPlace); // close up the redirected site pFile->Close(); delete pFile; pServer->Close(); delete pServer; if (bProgressMode) { cerr << _T("Caution: redirected to "); cerr << (LPCTSTR) strNewLocation << endl; } // figure out what the old place was if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort)) { cerr << _T("Error: the redirected URL could not be parsed.") << endl; ThrowTearException(2); } if (dwServiceType != INTERNET_SERVICE_HTTP) { cerr << _T("Error: the redirected URL does not reference a HTTP resource.") << endl; ThrowTearException(2); } // try again at the new location pServer = session.GetHttpConnection(strServerName, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags); pFile->AddRequestHeaders(szHeaders); pFile->SendRequest(); pFile->QueryInfoStatusCode(dwRet); if (dwRet != HTTP_STATUS_OK) { cerr << _T("Error: Got status code ") << dwRet << endl; ThrowTearException(2); } } cerr << _T("Status Code is ") << dwRet << endl; TCHAR sz[1024]; while (pFile->ReadString(sz, 1023)) { if (bStripMode) StripTags(sz); cout << sz; } // NOTE: Since HTTP servers normally spit back plain text, the // above code (which reads line by line) is just fine. However, // other data sources (eg, FTP servers) might provide binary data // which should be handled a buffer at a time, like this: #if 0 while (nRead > 0) { sz[nRead] = '\0'; if (bStripMode) StripTags(sz); cout << sz; nRead = pFile->Read(sz, 1023); } #endif pFile->Close(); pServer->Close(); } catch (CInternetException* pEx) { // catch errors from WinINet TCHAR szErr[1024]; pEx->GetErrorMessage(szErr, 1024); cerr << _T("Error: (") << pEx->m_dwError << _T(") "); cerr << szErr << endl; nRetCode = 2; pEx->Delete(); } catch (CTearException* pEx) { // catch things wrong with parameters, etc nRetCode = pEx->m_nErrorCode; TRACE1("Error: Exiting with CTearException(%d)\n", nRetCode); pEx->Delete(); } if (pFile != NULL) delete pFile; if (pServer != NULL) delete pServer; session.Close(); return nRetCode; }
DWORD CLoginDlg::login(LPCTSTR serverURL, LPCTSTR requestPage, LPCTSTR formData) { #ifdef _HTTPS CInternetSession session(_T("HelloChat"), INTERNET_FLAG_SECURE); CHttpConnection* pConnection = session.GetHttpConnection(serverURL, INTERNET_SERVICE_HTTP, INTERNET_DEFAULT_HTTPS_PORT); CString strHeaders = HEADER; // OPEN A POST REQUEST CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, requestPage, NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_COOKIES); #else CInternetSession session(_T("HelloChat"), PRE_CONFIG_INTERNET_ACCESS); CHttpConnection* pConnection = session.GetHttpConnection(serverURL); CString strHeaders = HEADER; // OPEN A POST REQUEST CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, requestPage, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_COOKIES); #endif // UNICODE #if defined(_UNICODE) // Length of form data (including '\\0') const int dwLength = _tcslen(formData) + 1; // Alocate ANSI character array char *lpFormData = new char[dwLength]; // Convert wide char form data to ANSI //멀티바이트코드로 변환. VERIFY(0 != ::WideCharToMultiByte(CP_ACP, 0, formData, -1, lpFormData, dwLength, 0, 0)); // Send the request to the HTTP server VERIFY(pFile->SendRequest(strHeaders, lpFormData, dwLength)); // Delete ANSI array delete[] lpFormData; // ANSI #else // Send the request to the HTTP server VERIFY(pFile->SendRequest(strHeaders, (LPVOID)formData, strlen(formData))); #endif // GET POST STATUS DWORD dwPostStatus = 0; VERIFY(pFile->QueryInfoStatusCode(dwPostStatus)); CString strBuffer = L""; BOOL brtn = pFile->ReadString(strBuffer); char* szBuff = LPSTR(LPCTSTR(strBuffer)); if (dwPostStatus == HTTP_STATUS_OK) { // 토큰 획득. BOOL bRtn = getToken(szBuff); if (!bRtn){ //토큰 획득 실패시. dwPostStatus = HTTP_STATUS_NO_CONTENT; //토큰 획득 실패 메시지 CComm func; func.WriteErrorLog(szBuff); } } else if (dwPostStatus == HTTP_STATUS_DENIED){ //토큰 Expire 확인. CComm func; BOOL bRtn = func.isTokenExpire(szBuff); if (bRtn){ //토큰 만료에 대한 로그 작성 func.WriteErrorLog(szBuff); // 토큰 Expire 토큰 재발급. GetPrivateProfileString(L"MYINFO", L"token", L"", (LPWSTR)func.m_myInfo.token, sizeof(func.m_myInfo.token), func.m_strINI); m_strMyToken = (LPCTSTR)(LPWSTR)func.m_myInfo.token; DWORD dwRtn = func.TokenRefresh(m_strMyToken); if (dwRtn == HTTP_STATUS_OK){ //토큰은 다시 재 발급 받았으므로 로그인 다시 시도 메시지. AfxMessageBox(L"Token Expired\r\nLogin Again"); } else{ //토큰 재 발급 과정에서 재 발급 실패. AfxMessageBox(L"Token Refresh Fail."); } } } pFile->Close(); return dwPostStatus; }
string GetData(const char * url, const string referer) { CInternetSession session("Mozilla/4.0"); session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10*1000); CHttpConnection * pServer = NULL; CHttpFile* pFile = NULL; bool bProgressMode = false; bool bStripMode = false; string line = ""; try { CString strServerName; CString strObject; INTERNET_PORT nPort; DWORD dwServiceType; if (!AfxParseURL(url, dwServiceType, strServerName, strObject, nPort) || dwServiceType != INTERNET_SERVICE_HTTP) { TRACE( _T("Error: can only use URLs beginning with http://") ); return "request failed - can only use URLs beginning with http://"; } if (bProgressMode) { TRACE("Opening Internet...%s\n", url); VERIFY(session.EnableStatusCallback(TRUE)); } pServer = session.GetHttpConnection(strServerName, nPort); string file = url; int file_start = -1; if(file.length() > 8) { file_start = (int)file.find("//"); file_start = (int)file.find("/", file_start+2); if(file_start > (int)file.length()) file_start = -1; } if(file_start > 0) file = file.substr(file_start); else file = ""; LPCTSTR ref = NULL; if(referer != "") ref = referer.c_str(); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,file.c_str(), ref); SetCookie(&session, url); pFile->SendRequest(); DWORD dwRet; pFile->QueryInfoStatusCode(dwRet); // if access was denied, prompt the user for the password if (dwRet == HTTP_STATUS_DENIED) { DWORD dwPrompt; dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD, FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL); // if the user cancelled the diaTRACE, bail out if (dwPrompt != ERROR_INTERNET_FORCE_RETRY) { TRACE( _T("Access denied: Invalid password")); return "request failed - Access denied: Invalid password"; } pFile->SendRequest(); pFile->QueryInfoStatusCode(dwRet); } CString strNewLocation; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); // were we redirected? // these response status codes come from WININET.H if (dwRet == HTTP_STATUS_MOVED || dwRet == HTTP_STATUS_REDIRECT || dwRet == HTTP_STATUS_REDIRECT_METHOD) { CString strNewLocation; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation); int nPlace = strNewLocation.Find(_T("Location: ")); if (nPlace == -1) { TRACE( _T("Error: Site redirects with no new location") ); return "request failed - Site redirects with no new location"; } strNewLocation = strNewLocation.Mid(nPlace + 10); nPlace = strNewLocation.Find('\n'); if (nPlace > 0) strNewLocation = strNewLocation.Left(nPlace); // close up the redirected site pFile->Close(); delete pFile; pServer->Close(); delete pServer; if (bProgressMode) { TRACE( _T("Caution: redirected to ")); TRACE( (LPCTSTR) strNewLocation ); } // figure out what the old place was if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort)) { TRACE( _T("Error: the redirected URL could not be parsed.") ); return "request failed - the redirected URL could not be parsed."; } if (dwServiceType != INTERNET_SERVICE_HTTP) { TRACE( _T("Error: the redirected URL does not reference a HTTP resource.") ); return "request failed - the redirected URL does not reference a HTTP resource"; } // try again at the new location pServer = session.GetHttpConnection(strServerName, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject); pFile->SendRequest(); pFile->QueryInfoStatusCode(dwRet); if (dwRet != HTTP_STATUS_OK) { //TRACE( _T("Error: Got status code ") dwRet ); return "request failed - Got status code " + dwRet; } } TCHAR sz[1024]; sz[1023] = 0; while (pFile->ReadString(sz, 1023)) { line += sz; } SetCookie(&session, strServerName); SetCookie(&session, url); } catch (exception e) { TRACE(e.what()); line = "request failed - status code N/A"; } catch (CInternetException *e) { TCHAR error[256]; if(e->GetErrorMessage(error, 256)) { TRACE(error); } e->Delete(); e = NULL; line = "request failed - status code N/A"; } if(pServer != NULL) { pServer->Close(); delete pServer; pServer = NULL; } if(pFile != NULL) { pFile->Close(); delete pFile; pFile = NULL; } return line; }
// 初始化更新列表 BOOL CUpdaterApp::_InitFileList() { BOOL bReturn(FALSE); CString strFileList(m_szServer); strFileList += _T("/Filelist.txt"); CInternetSession sess; sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000); sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 5000); sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000); sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5000); sess.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); try { CHttpFile* pHttpFile = (CHttpFile*)sess.OpenURL(strFileList, 1, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_TRANSFER_ASCII, NULL, 0); if (pHttpFile) { DWORD dwErrCode(0); pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, dwErrCode); if (dwErrCode >= 200 && dwErrCode < 300) { CStringA strLineA; while (pHttpFile->ReadString((LPTSTR)strLineA.GetBuffer(512), 511)) { strLineA.ReleaseBuffer(); // MFC-bug CString strLine(strLineA); strLine.TrimLeft(); if (strLine.GetLength() > 0 && strLine[0] != _T('\\')) { continue; } std::vector<CString> vecStrs = Split(strLine, _T("^^^^^^")); if (vecStrs.size() >= 2) { LPUPDATEITEM pUpdateItem = new UPDATEITEM; ZeroMemory(pUpdateItem, sizeof(*pUpdateItem)); lstrcpyn(pUpdateItem->szFileName, vecStrs[0], MAX_PATH); lstrcpyn(pUpdateItem->szServerMD5, vecStrs[1], 33); m_arrUpdate.Add(pUpdateItem); } } pHttpFile->Close(); sess.Close(); bReturn = TRUE; } else { LOG(_T("网站访问错误码:%d"), dwErrCode); } } } catch (...) { LOG(_T("下载列表异常!")); } return bReturn; }
HRESULT CMainFrame::GetWMEVersionHttp(CString& WMEVersion) { HRESULT RetCode = S_OK; WMEVersion = "0.0.0"; CString Magic = GetRegString(HKEY_CURRENT_USER, DCGF_TOOLS_REG_PATH, "BBID"); CInternetSession Session; CHttpConnection* Server = NULL; CHttpFile* File = NULL; DWORD HttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT; const TCHAR Headers[] = _T("Accept: text/*\r\nUser-Agent: WME ProjectMan\r\n"); CString Url = LOC("/str1086/http://www.dead-code.org/vercheck.php"); CString CurrVer; CurrVer.Format("%d.%d.%03d", DCGF_VER_MAJOR, DCGF_VER_MINOR, DCGF_VER_BUILD); Url += "?product=wme&ver=" + CurrVer; Url += "&bbid=" + Magic; if(DCGF_VER_BETA) Url += "&beta=1"; bool DotNet = RegKeyExists(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\.NETFramework\\policy\\v2.0"); Url += "&dotnet=" + CString(DotNet?"1":"0"); CString ServerName; CString Object; INTERNET_PORT Port; DWORD ServiceType; try{ if(!AfxParseURL(Url, ServiceType, ServerName, Object, Port) || ServiceType != INTERNET_SERVICE_HTTP){ return E_FAIL; } Server = Session.GetHttpConnection(ServerName, Port); File = Server->OpenRequest(CHttpConnection::HTTP_VERB_GET, Object, NULL, 1, NULL, NULL, HttpRequestFlags); File->AddRequestHeaders(Headers); if(File->SendRequest()){ TCHAR sz[1024]; if(File->ReadString(sz, 1023)){ WMEVersion = Entry(1, CString(sz), '\n'); } } File->Close(); Server->Close(); } catch (CInternetException* pEx) { // catch errors from WinINet //TCHAR szErr[1024]; //pEx->GetErrorMessage(szErr, 1024); //MessageBox(szErr, LOC("/str1002/Error"), MB_OK|MB_ICONERROR); RetCode = E_FAIL; pEx->Delete(); } if (File != NULL) delete File; if (Server != NULL) delete Server; Session.Close(); return RetCode; }
int CHttpRequest::ConnectUrl(TCHAR *sUrl, TCHAR *sReturn, long *lVersion, int *Count) { ODS(_T("XFILTER.EXE: GetFromUrl Begin... \n")); if(sUrl == NULL) return XERR_INVALID_PARAMETER; CString strServerName; CString strObject; INTERNET_PORT nPort; DWORD dwServiceType; if (!AfxParseURL(sUrl, dwServiceType, strServerName, strObject, nPort) || dwServiceType != INTERNET_SERVICE_HTTP) { ODS(_T("XFILTER.EXE: Internet Invalid Url ...")); return XERR_INTERNET_URL_ERROR; } CInternetSession session(GUI_APP_CLASS_NAME); CHttpConnection *pServer = NULL; CHttpFile *pFile = NULL; int iRet = XERR_SUCCESS; m_IsConnecting = TRUE; try { pServer = session.GetHttpConnection(strServerName, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject); pFile->AddRequestHeaders(szHeaders); pFile->SendRequest(); DWORD dwRet; pFile->QueryInfoStatusCode(dwRet); if (dwRet >= 400 && dwRet <= 499) { ODS(_T("XFILTER.EXE: Internet Request Error ...")); iRet = XERR_INTERNET_REQUEST_ERROR; } else if(dwRet >= 500 && dwRet <= 599) { ODS(_T("XFILTER.EXE: Internet Server Error ...")); iRet = XERR_INTERNET_SERVER_ERROR; } else if(sReturn != NULL) { pFile->ReadString(sReturn, MAX_NET_COMMAND_LENTH - 1); ODS(sReturn); CString tmpStr = sReturn; long lVer = atol(tmpStr.Left(MAX_NET_COMMAND_VERSION_LENTH)); if(lVer > *lVersion) { *lVersion = lVer; int i = 1; while (i < MAX_NET_COMMAND && pFile->ReadString((sReturn + MAX_NET_COMMAND_LENTH * i), MAX_NET_COMMAND_LENTH - 1)) { ODS(sReturn + i * MAX_NET_COMMAND_LENTH); i ++; } *Count = i; } else { *Count = 1; } } else { CString sRet; pFile->ReadString(sRet); if(sRet.GetAt(0) != '1') iRet = XERR_INTERNET_REG_ERROR; ODS2(_T("XFILTER.EXE: Internet User Register Return Value "),sRet); } pFile->Close(); pServer->Close(); } catch(CInternetException* pEx) { pEx->Delete(); iRet = XERR_INTERNET_CONNECT_ERROR; ODS(_T("XFILTER.EXE: GetFromUrl XERR_INTERNET_CONNECT_ERROR... ")); } if (pFile != NULL) delete pFile; if (pServer != NULL) delete pServer; session.Close(); m_IsConnecting = FALSE; ODS(_T("XFILTER.EXE: GetFromUrl End... ")); return iRet; }
CString GetServerDetailInfo(CString strServer, CString &strTitle) { strServer.Insert(0, "http://"); CString strReturn = "Unknown", szAllData, szData; CInternetSession ss(_T("session"), 0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);//设置不缓冲 CHttpFile *pF = NULL; try { ss.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10 * 1000); pF = (CHttpFile *)ss.OpenURL(strServer, 1, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_NO_AUTO_REDIRECT); pF->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, szAllData, 0); int nPos1 = szAllData.Find("Server: ", 0); if (nPos1 == -1) { strReturn = "Unknown"; } else { nPos1 += 8; int nPos2 = szAllData.Find("\r\n", nPos1); strReturn = szAllData.Mid(nPos1, nPos2 - nPos1); if (strReturn.Find("Microsoft", 0) != -1) { strReturn.Insert(0, "Windows, "); } } szAllData.Empty(); while(pF->ReadString(szData)) szAllData += szData; szAllData.MakeLower(); nPos1 = szAllData.Find("<title>", 0); if (nPos1 != -1) { int nPos2 = szAllData.Find("</title>", nPos1); if (nPos2 != -1) { strTitle = szAllData.Mid(nPos1 + 7, nPos2 - nPos1 - 7); if (IsTextUTF8(strTitle.GetBuffer(0), strTitle.GetLength())) Utf8ToAnsi(strTitle); } } } catch(...) { strReturn = ""; } if (pF != NULL) { pF->Close(); delete pF; pF = NULL; } if (ss != NULL) { ss.Close(); delete ss; } return strReturn; }
int getIndexData(void) { CInternetSession mySession(NULL,0); CHttpFile* myHttpFile = NULL; CString myData; CString myURL; int i = 0; myURL = m_URL + _T(A50ID) + _T(HS300ID); try{ myHttpFile = (CHttpFile*)mySession.OpenURL(myURL); } catch(CInternetException*pException){ pException->Delete(); return -1;//读取失败,返回 } if(myHttpFile != NULL){ try{ while(myHttpFile->ReadString(myData)) { CString strGet1(_T("")); CString strGet2(_T("")); CString strGet3(_T("")); double temp = 0; AfxExtractSubString(strGet1,myData,1, _T('\"')); AfxExtractSubString(strGet2,strGet1,3, _T(','));//现在的价格 AfxExtractSubString(strGet3,strGet1,2, _T(','));//昨天的价格 LPTSTR chValue = strGet2.GetBuffer( strGet2.GetLength() ); LPTSTR chValueZT = strGet3.GetBuffer( strGet3.GetLength() ); double fValue = atof(chValue); //今天的价格 double fZT = atof(chValueZT);//昨天的价格 strGet2.ReleaseBuffer(); if(fValue > 0.1){//防止等于0,等于0就用昨天的收盘价 price[i] = fValue; } else{ price[i] = fZT; } i++; } } catch(CInternetException*pException){ pException->Delete(); return -1;//返回 } } //计算A50和HS300指数 double totalValueA50 = 0; double totalValueHS300 = 0; for(int i = 0;i < TOTAL;i++){ if(i < A50NUM){ totalValueA50 = totalValueA50 + price[i] * volume[i]; } if(i >= A50NUM){ totalValueHS300 = totalValueHS300 + price[i] * volume[i]; } } ::EnterCriticalSection(&g_index); A50Index = A50IndexRef * totalValueA50 / A50totalVolumeRef; HS300Index = HS300IndexRef * totalValueHS300 / HS300totalVolumeRef; ::LeaveCriticalSection(&g_index); myHttpFile->Close(); delete myHttpFile; mySession.Close(); return 0; }