MFC网页访问的实现示例

本示例使用MFC 类CInternetSession 建立连接,使用 CHttpFile读取内容。


首先,建立一个MFC对话框项目,界面如下:


1. 添加头文件:

#include <afxinet.h>


2. UTF-8 转 UNICODE:

int cU8xU(wchar_t* pOut,char *pText)
{
	int ret = 0; 
	char* uchar = (char *)pOut; 
	unsigned cIn = (unsigned char)pText[0]; 
	if(cIn<0x80){ // ASCII 0x00 ~ 0x7f 
		pOut[0] = pText[0]; 
	}else if(cIn<0xdf){ 
		uchar[0] = (pText[0]<<6)|(pText[1]&0x3f); 
		uchar[1] = (pText[0]>>2)&0x0f; 
		ret = 1; 
	}else if(cIn<=0xef){ 
		uchar[0] = (pText[1]<<6)|(pText[2]&0x3f); 
		uchar[1] = (pText[0]<<4)|((pText[1]>>2)&0x0f); 
		ret = 2; 
	}else if(cIn<0xf7){ 
		uchar[0] = (pText[2]<<6)|(pText[3]&0x3f); 
		uchar[1] = (pText[1]<<4)|((pText[2]>>2)&0x0f); 
		uchar[2] = ((pText[0]<<2)&0x1c)|((pText[1]>>4)&0x03); 
		ret = 3; 
	} 
	return ret; 
}
int sU8xU(WCHAR* pOut,char *pText,int Len){ 
	int i,j; 
	for(i=0,j=0;i<Len;i++,j++){ 
		i+=cU8xU(&pOut[j],&pText[i]); 
	} 
	return j; 
}


3. 加入按键响应函数:

void CURL_TESTDlg::OnBnClickedButton1()
{
//#include <afxinet.h>
	CString url;
	GetDlgItem(IDC_EDIT1)->GetWindowText(url); 

	DWORD code = 0;
	CInternetSession httpSession ; 
	CHttpFile * htmlFile=NULL ;

	try{
		//打开网页 
		htmlFile=(CHttpFile*)httpSession.OpenURL(_T("http://www.sogou.com/")); 
		
		//htmlFile=(CHttpFile*)httpSession.OpenURL(url);
		htmlFile->QueryInfoStatusCode(code);
	}catch(...)
	{
		MessageBox(_T("err"),_T("BE"),1); // 如果断网而打不开这个网址,并且不抓取这个错误,就会弹出一个提示系统提示框!这里我自己处理。

		return;
	}

	//读取网页数据 
	CString str;
	CString info=_T("");
	while(htmlFile->ReadString(str))
	{   
		info+=str ;
	}

	//释放
	htmlFile->Close();
	httpSession.Close();

	//显示
	CString result;
	int length = info.GetLength();
	char *ab = new char[length];
	memset(ab, 0, length);
	memcpy(ab, info.GetBuffer(), length);
	WCHAR *big = new WCHAR[length];
	memset(big, 0, length);
	int len = sU8xU(big, ab, length);
	big[len] = '\0';
	delete []ab;
	
	CString temp(big);

	result.Format("The return code(返回值): %d\r\n %s", code, temp.GetBuffer());

	GetDlgItem(IDC_EDIT2)->SetWindowText(result); 
	//delete []big; // i don't how to do for this.
}


4. 另外也可以使用这段粗糙的代码实现这个功能:

	CInternetSession mysession;
	CHttpConnection *myconn=mysession.GetHttpConnection("www.veno2.com");

	CHttpFile *myfile=myconn->OpenRequest("GET","/ag_list");
	myfile->SendRequest();

	CString mystr;
	CString tmp;
	while(myfile->ReadString(tmp))
	{
		mystr+=tmp;
	}
	//GetDlgItem(IDC_EDIT2)->SetWindowText(mystr); 

	myfile->Close();
	myconn->Close();
	mysession.Close();
	delete myfile;
	delete myconn;
	myfile=0;
	myconn=0;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vector<CString> vecList; vecList.push_back(_T("110.77.0.3:80 ")); vecList.push_back(_T("106.60.94.127:80 ")); vecList.push_back(_T("49.94.32.45:80 ")); vecList.push_back(_T("119.188.94.145:80 ")); vecList.push_back(_T("37.49.137.243:80 ")); vecList.push_back(_T("111.13.132.92:80 ")); vecList.push_back(_T("49.94.148.119:80 ")); vecList.push_back(_T("49.94.164.209:80 ")); vecList.push_back(_T("49.94.2.84:80 ")); vecList.push_back(_T("49.90.1.251:80 ")); vecList.push_back(_T("117.37.243.194:80 ")); vecList.push_back(_T("110.4.12.175:80 ")); vecList.push_back(_T("36.98.30.234:80 ")); vecList.push_back(_T("110.244.111.56:80 ")); vecList.push_back(_T("49.94.131.174:80 ")); vecList.push_back(_T("111.1.61.23:80 ")); vecList.push_back(_T("54.252.97.45:80 ")); vecList.push_back(_T("111.13.12.202:80 ")); vecList.push_back(_T("106.33.2.219:80 ")); vecList.push_back(_T("110.176.86.5:80 ")); vecList.push_back(_T("171.9.147.106:80 ")); vecList.push_back(_T("200.33.27.33:80 ")); vecList.push_back(_T("49.94.141.104:80 ")); vecList.push_back(_T("110.176.178.131:80 ")); vecList.push_back(_T("49.93.24.73:80 ")); vecList.push_back(_T("111.12.128.166:80 ")); vecList.push_back(_T("223.15.233.54:80 ")); vecList.push_back(_T("123.173.206.192:80 ")); vecList.push_back(_T("110.77.197.132:80 ")); vecList.push_back(_T("49.94.0.196:80 ")); WebClient wc; WebClient wc2; CString strData=_T(""); for(int i=0;i<vecList.size();i++) { try { CString str1; CString strIP=vecList[i]; strIP.Trim(); wc.SetProxy(strIP); CString strTemp=_T(""); wc.Get(_T("http://www.2345.com/?24384-1217"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); strData+=str1; wc2.SetProxy(strIP); wc.Get(_T("http://www.haosooo.cn/?f=me"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); CSuperLog::WriteLog(str1); Sleep((1000)); } catch (...) { } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值