#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);
}