⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webclient.cpp

📁 BQYAHOO的Visual C++源代码
💻 CPP
字号:

//本页源代码实现了所有与HTTP相关的功能,相当于一个简陋的HTTP客户端。

#include "StdAfx.h"
#include "WebClient.h"


bool WebClient::BeforeLogIn()//进入登陆页面
{
	hsession=::InternetOpen("biqiong",INTERNET_OPEN_TYPE_PRECONFIG ,NULL ,NULL,NULL);
	hconnect=::InternetOpenUrl(hsession,BeforLogInUrl.c_str (),NULL,NULL,CONNECT_FLAG,NULL);

	GetResponse();
	GetHead();	
	
	xlog.log ("进入登陆页面");//记录日子信息
	xlog.log (head);
	xlog.log (response);
	
	if(TestResponse()==false) return false;
	if(strstr(response,"ShowFolder?rb=Inbox" )!=NULL) MayBeIn=true;//目前已经登陆成功

	return true;

}

bool WebClient::LogIn(const char * LogInUrl , const string UserName)
{
	if(MayBeIn==true) return true;
	
	hconnect=::InternetOpenUrl(hsession,LogInUrl,NULL,NULL,CONNECT_FLAG,NULL);
	GetResponse();
	GetHead();

	xlog.log ("登陆之后");//记录日子信息
	xlog.log (head);
	xlog.log (response);

	if(TestResponse()==false) return false;
	if(strstr(response,"登录失败" )!=NULL) return false;
	
	return true;


}

bool WebClient::LogOut(string LogOutUrl)
{
	if(hconnect==NULL) return false;
	hconnect=::InternetOpenUrl(hsession,LogOutUrl.c_str (),NULL,NULL,CONNECT_FLAG,NULL);

	GetResponse();
	GetHead();	

	memset(response,0,sizeof(response));
	memset(head,0,sizeof(head));

	return true;
}

bool WebClient::InMail(const string MailUrl)
{
	if(MayBeIn==true) return true;
	hconnect=::InternetOpenUrl(hsession,MailUrl.c_str (),NULL,NULL,CONNECT_FLAG,NULL);
	

	GetResponse();
	GetHead();	


	xlog.log ("进入邮箱");//记录日志信息
	xlog.log (head);
	xlog.log (response);


	if(TestResponse()==false) return false;
	
	return true;
}

bool WebClient::InInBox(const string MailHostUrl)
{
	
	string InBoxUrl;
	InBoxUrl=MailHostUrl+"ym/ShowFolder?rb=Inbox&box=Inbox";

	hconnect=::InternetOpenUrl(hsession,InBoxUrl.c_str (),NULL,NULL,CONNECT_FLAG,NULL);

	GetResponse();
	GetHead();	

	xlog.log ("进入收件箱");//记录日子信息
	xlog.log (head);
	xlog.log (response);


	if(TestResponse()==false) return false;
	
	return true;
}




WebClient::WebClient()
{
	hsession=0;
	hconnect=0;
	hrequest=0;


	CONNECT_FLAG = INTERNET_FLAG_NO_CACHE_WRITE;
	BeforLogInUrl="http://cn.mail.yahoo.com/";//雅虎中国的邮箱登录页面


	MayBeIn=false;

}



WebClient::~WebClient()
{
	
	::InternetCloseHandle(hsession);
	::InternetCloseHandle(hconnect);
	::InternetCloseHandle(hrequest);
}



inline void WebClient::GetResponse()
{
	unsigned long all=0;
	memset(response,0,sizeof(response));

	numberOfBytesRead=0;
	while(InternetReadFile(hconnect,response+all,5*1024,&numberOfBytesRead)==true)
	{
		all=all+numberOfBytesRead;
		if(numberOfBytesRead==0)break;
	}
}


inline void  WebClient::GetHead()//对HTTP头信息进行检查
{
	int n=sizeof(head);
	int m=0;
	memset(head,0,sizeof(head));
	HttpQueryInfo(hconnect,HTTP_QUERY_RAW_HEADERS_CRLF ,head,(LPDWORD)&n,(LPDWORD)&m);
}


inline bool  WebClient::TestResponse()//对HTTP回复信息进行检查
{
	if(strlen(response)==0) //是否为空
	{	
		return false;
	}
	if(strstr(response,"</html>")==NULL) //是否结束
	{		
		return false;
	}
	return true;
}


bool WebClient::DownLoadFile(string FileSaveName,string DownLoadUrl)
{
	//所有下载操作,最终由以下代码执行。
	//虽然简单粗俗,却很是实用。
	const unsigned long CacheSize=100*1024;//分块下载,每一块100K
	HINTERNET hfile;
	char cache[CacheSize];
	unsigned long byteofread=CacheSize;
	FILE *fdest;

	fdest=fopen(FileSaveName.c_str (),"wb");	
	hfile=::InternetOpenUrl(hsession ,DownLoadUrl.c_str (),NULL,NULL,NULL,NULL);	
	while(::InternetReadFile(hfile,cache,CacheSize,&byteofread)==true)
	{				
		fwrite(cache ,sizeof(char),byteofread ,fdest);
		if(byteofread==0)break;
	}	
	fclose(fdest);

	
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -