#include <afxinet.h> #include <afxwin.h> #include <iostream> #include <fstream> #include <string> #pragma comment(lib,"wininet.lib") using namespace std; CString content; bool getData(LPCTSTR proxy) { CString data; DWORD dwStatusCode; CInternetSession session("HttpClient"); CHttpFile *pfile = NULL; INTERNET_PROXY_INFO proxyinfo; proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY; proxyinfo.lpszProxy = proxy; proxyinfo.lpszProxyBypass = NULL; session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,sizeof(INTERNET_PROXY_INFO)); session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000); // 2秒的连接超时 session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); // 1秒的发送超时 session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000); // 7秒的接收超时 session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000); // 1秒的发送超时 session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 7000); // 7秒的接收超时 session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); // 1次重试 try { pfile = (CHttpFile*)session.OpenURL("http://ip.dnsexit.com/", 1, INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE); } catch (CInternetException *e) { pfile = NULL; e->Delete(); session.Close(); return false; } pfile->QueryInfoStatusCode(dwStatusCode); if(dwStatusCode == HTTP_STATUS_OK) { while (pfile->ReadString(data)) { content += data; } pfile->Close(); session.Close(); return true; } else { return false; } } int main() { string line; fstream fin("proxy.txt"); while (getline(fin, line)) { content.Empty(); if (getData(line.c_str())) { cout << content << "is OK!" << endl; } } fin.close(); return 0; }
我们可以利用代理IP来刷访问量等等。