




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、最近做了个项目,用VC开发,很有技术难度,习惯了Java开发,VC都忘差不多啦,呵呵 为了吃饭,硬着头皮也得做下去啊。项目里面用到了获取网页数据,需要http header 来验证客户端合法性,找了些资料,都有问题。还是自己写吧,用 CInternetSession/头部包含afxinet.h #include <afxinet.h>CInternetSession:OpenURL函数原型为: CStdioFile* OpenURL( LPCTSTR pstrURL, /文件URL地址 DWORD dwContext = 1, /上下文ID DWO
2、RD dwFlags = INTERNET_FLAG_TRANSFER_ASCII, /标记 LPCTSTR pstrHeaders = NULL, /发送到服务器的数据头 DWORD dwHeadersLength = 0 );/发送到服务器的数据头长度dwFlags可以为:INTERNET_FLAG_RELOAD 强制重读数据 INTERNET_FLAG_DONT_CACHE 不保存到缓存 INTERNET_FLAG_TRANSFER_ASCII 使用文本数据 INTERNET_FLAG_TRANSFER_BINARY 使用二进制数据 /获取网页的方法代码如下CSt
3、ring getURLContext(LPCTSTR pstrURL) /自定义http头信息 char* headers="Accept:*/*rn" "Accept-Language:zh-cnrn" "User-Agent:VCTestClientrn" CString m_strHTML; try CInternetSession sess;/建立会话 CHttpFile* pF=(CHttpFile*)se
4、ss.OpenURL(pstrURL,1,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD,headers,strlen(headers);/打开文件 注意:OpenURL 方法的dwFlags参数 必须选择传输模式为INTERNET_FLAG_TRANSFER_ASCII 或者 INTERNET_FLAG_TRANSFER_BINARY CString szData,szAllData; while(pF->ReadString(szData) &
5、#160; /读取文件 szAllData+="rn" szAllData+=szData; pF->Close(); sess.Close(); m_strHTML=szAllData; catch(CException *e) AfxMessageBox("获取数据失败"); return
6、m_strHTML;希望能对需要的朋友有帮助。/下载文件的方法/url:网络文件地址,:保存到磁盘的文件名bool Down url,CString ) bool result=false; try /自定义http头信息 char* headers="Accept:*/*rn" "Accept-Language:zh-cnrn" "User-Agent:VCTestClientrn" HINTERNET hNet = InternetOpen("Ou
7、tlook",PRE_CONFIG_INTERNET_ACCESS,NULL,INTERNET_INVALID_PORT_NUMBER,0) ; HINTERNET hUrlFile = InternetOpenUrl(hNet,url,headers,strlen(headers),INTERNET_FLAG_RELOAD,0); char buffer10*1024 ; DWORD dwBytesRead = 1; BOOL bRead=TRUE; C; ();/创建本地文件,准备写入数据 while(bRea
8、d&&dwBytesRead>0) bRead = InternetRead(buffer),&dwBytesRead); if(dwBytesRead>0) (buffer,dwBytesRead); InternetCloseHandle(hUrlFile) ; InternetCloseHandle(hNet) ; (); result=true; catch(CException *e) result=false;
9、160; return result; 利用CInternetSession从网站获取信息,并利用CString拆分查找特定信息2008-6-3 网络点击:58 评论 -#include "stdafx.h"#include "HttpRequest.h"#include "HttpRequestDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/*/ CHttpRequestDl
10、g dialogCHttpRequestDlg:CHttpRequestDlg(CWnd* pParent /*/*=NULL*/) : CDialog(CHttpRequestDlg:IDD, pParent). /AFX_DATA_INIT(CHttpRequestDlg) /AFX_DATA_INIT / Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CHttpRequestDlg:Do
11、DataExchange(CDataExchange* pDX). CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CHttpRequestDlg) DDX_Control(pDX, IDC_TITLE_END, m_titleend); DDX_Control(pDX, IDC_TITLE_START, m_titlestart); DDX_Control(pDX, IDC_HREF_LIST_END, m_hreflistend); DDX_Control(pDX, IDC_HREF_LIST_START, m_hrefliststart); DDX_
12、Control(pDX, IDC_MSG, m_msg); DDX_Control(pDX, IDC_HTTP_ADDR, m_httpaddr); DDX_Control(pDX, IDC_HREF_START, m_hrefstart); DDX_Control(pDX, IDC_HREF_END, m_hrefend); DDX_Control(pDX, IDC_CON_START, m_constart); DDX_Control(pDX, IDC_CON_END, m_conend); /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CHttpRequestDlg, C
13、Dialog) /AFX_MSG_MAP(CHttpRequestDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_START_MSG, OnStartMsg) /AFX_MSG_MAPEND_MESSAGE_MAP()/*/ CHttpRequestDlg message handlersBOOL CHttpRequestDlg:OnInitDialog(). CDialog:OnInitDialog(); / Set the icon for this dialog. The framework does this aut
14、omatically / when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); / Set big icon SetIcon(m_hIcon, FALSE); / Set small icon / TODO: Add extra initialization here /*/ /先初始化/ m_httpaddr.SetWindowText(""); m_hrefliststart.SetWindowText(" <td class=fontindex14p
15、x width=643 bgColor=#ffffff><STRONG>深圳新闻</STRONG></td>"); m_hreflistend.SetWindowText(" <table cellSpacing=0 cellPadding=0 width=650 align=center border=0>"); m_hrefstart.SetWindowText("href=""); m_hrefend.SetWindowText("""); m_
16、titlestart.SetWindowText("<title>"); m_titleend.SetWindowText("_深圳新闻_深圳人</title>"); m_constart.SetWindowText("<td class=news-zhengwenneirong>"); m_conend.SetWindowText("</P></P></td>"); /*/ return TRUE; / return TRUE unless
17、you set the focus to a control/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CHttpRequestDlg:OnPaint() . if (IsIconic() . CPaintDC dc(this); /
18、 device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect
19、.Height() - cyIcon + 1) / 2; / Draw the icon dc.DrawIcon(x, y, m_hIcon); else . CDialog:OnPaint(); / The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CHttpRequestDlg:OnQueryDragIcon(). return (HCURSOR) m_hIcon;void CHttpRequestDlg:OnCancel() .
20、/ TODO: Add extra cleanup here CDialog:OnCancel();void CHttpRequestDlg:OnOK() . / TODO: Add extra validation here /CDialog:OnOK();/检测用户所填写的是否为空BOOL CHttpRequestDlg:checkedit(). BOOL Cando=true; if(m_httpaddr.GetWindowTextLength()=0) Cando=false; if(m_hrefliststart.GetWindowTextLength()=0) Cando=fals
21、e; if(m_hreflistend.GetWindowTextLength()=0) Cando=false; if(m_hrefstart.GetWindowTextLength()=0) Cando=false; if(m_hrefend.GetWindowTextLength()=0) Cando=false; return Cando;/*/获取首页信息/void CHttpRequestDlg:OnStartMsg() . if(!checkedit() . MessageBox("请填写完整信息!",NULL,MB_OK); else . CInternet
22、Session mySession(NULL,0); CHttpFile* myHttp; CString newsdata; m_httpaddr.GetWindowText(newsdata); myHttpFile=(CHttpFile*)mySession.OpenURL(newsdata);/打开连接 CString myData; newsdata = "" while(myHttpFile->ReadString(myData)/循环读文件信息 . newsdata=newsdata+" " newsdata+=myData; myH
23、ttpFile->Close() ; mySession.Close() ; /m_msg.SetWindowText(newsdata); PrintHref(newsdata);/获取新闻连接列表/ /获取新闻连接列表/void CHttpRequestDlg:PrintHref(CString printstr). CString str=printstr,strtemp,strnew,hrefstr="" int istart=0,iend=0; /获取链表范围 m_hrefliststart.GetWindowText(strtemp); istart =
24、str.Find(strtemp); m_hreflistend.GetWindowText(strtemp); iend = str.Find(strtemp); strnew = str.Mid(istart,iend-istart); /在链表范围内循环读新闻连接 for(int i=0;i<8;i+). m_hrefstart.GetWindowText(strtemp); istart = strnew.Find(strtemp); istart = istart+strtemp.GetLength(); strnew = strnew.Mid(istart); m_hrefe
25、nd.GetWindowText(strtemp); iend = strnew.Find(strtemp); hrefstr = hrefstr+strnew.Mid(0,iend)+" " m_msg.SetWindowText(hrefstr); PrintCon(hrefstr);/分析新闻连接列表/分析新闻连接列表/void CHttpRequestDlg:PrintCon(CString constr). CString str=constr,strtemp,strnew; int istart=0,iend=0; for(int i=0;i<8;i+).
26、 istart = str.Find(" "); strnew = str.Left(istart); /*/*/ /*/*对每一个连接分析内容*/ /*/*/ GetNewsCon(strnew);/获取一个新闻内容/ /*/*/ istart = istart + 2; str = str.Mid(istart); /m_msg.SetWindowText(str);/获取一个新闻内容/void CHttpRequestDlg:GetNewsCon(CString NewsUrl). CInternetSession mySession(NULL,0); CHttpFile* myHttp; myHttpFile=(CHttpFile*)mySession.OpenURL(NewsUrl); CString myData; CString newsdata = "" while(myHttpFile->ReadString(myData) . newsdata=newsdata+&qu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广告字施工合同样本模板3篇
- 意向金合同模板3篇
- 展览布展工程招标指南3篇
- 工程勘察招标文件编写范本3篇
- 合作协议范本汇编3篇
- 保证书模板保证策略3篇
- 交易委托授权书3篇
- 借读生学业保障书3篇
- 户口登记授权书3篇
- 入伍家长诚信保证书3篇
- (2024年)肺栓塞课件
- 2024吉林省民航机场集团有限公司招聘笔试参考题库附带答案详解
- 电磁现象及其应用-理解电磁现象及其在日常生活中的应用
- 车辆行驶安全培训模板
- 开展中医药健康文化宣传活动方案(样式)
- 油漆涂料行业市场分析
- 呼吸道合胞病毒知识科普
- 跨境数据流动与治理
- 输血治疗知情同意书
- 幼儿园副园长聘任园长合同(36篇)
- 30道中国石油天然气地球物理勘探工程师岗位常见面试问题含HR常问问题考察点及参考回答
评论
0/150
提交评论