#include "stdafx.h"
#include "ACCBLOG.h"
#include "afxinet.h"
#include "afx.h"
#include <fstream>
#include <process.h>
#include <vector>
//#include <stdlib.h>
//#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define random(x) (rand()%x)
// 唯一的应用程序对象
static int count = 0;
CWinApp theApp;
using namespace std;
unsigned int _stdcall testHttp(void *p);
bool AccessMyBlog(CString strHttpName);
void getBlogUrl();
HANDLE hEvent;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
hEvent = CreateEvent(NULL,true,false,NULL);
getBlogUrl();
unsigned int a;
_beginthreadex(NULL,0,testHttp,0,0,&a);
}
Sleep(5000000000000);
return nRetCode;
}
bool AccessMyBlog(CString strHttpName)
{
CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = "Content-Type: application/x-www-form-urlencoded"; // 请求头
try
{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
if(fileGet)
{
count++;
cout<<count<<":访问博客("<<strHttpName<<")成功"<<endl;
}
else
{
cout<<"访问博客("<<strHttpName<<")失败"<<endl;
}
}
catch(CException* e)
{
fileGet = 0;
throw;
}
}
//
vector<CString> m_pBlog_vex;
unsigned int _stdcall testHttp(void *p)
{
WaitForSingleObject(hEvent,INFINITE);
vector<CString>::iterator itercsVec = m_pBlog_vex.begin();
while(1)
{
while (itercsVec != m_pBlog_vex.end())
{
CString strUrl = *itercsVec;
AccessMyBlog(strUrl);
itercsVec++;
Sleep(10);
}
itercsVec = m_pBlog_vex.begin();
Sleep(random(10)*20);
}
return 0;
}
void getBlogUrl()
{
char *iniBlogUrl = "http://blog.csdn.net";
char buffer[256];
ifstream myfile;//
myfile.open("blog.txt");
if(myfile)
{
while (!myfile.eof() )
{
CString myUrl;
myfile.getline (buffer,256);
myUrl.Format("%s%s",iniBlogUrl,buffer);
m_pBlog_vex.push_back(myUrl);
}
}
myfile.close();
SetEvent(hEvent);
}