您的位置:首页 > 运维架构 > 网站架构

利用CInternetSession从网站获取信息,并利用CString拆分查找特定信息

2008-06-02 23:17 507 查看
getsockname

CInternetSession::OpenURL
internetopenurl
connect
SockGetHostByName 此4个为连接地址的函数 它们都会用到url 或者远程的地址


#include "stdafx.h"


#include "HttpRequest.h"


#include "HttpRequestDlg.h"




#ifdef _DEBUG


#define new DEBUG_NEW


#undef THIS_FILE


static char THIS_FILE[] = __FILE__;


#endif






/**//////////////////////////////////////////////////////////////////////////////


// CHttpRequestDlg dialog






CHttpRequestDlg::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::DoDataExchange(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_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_MAP


}




BEGIN_MESSAGE_MAP(CHttpRequestDlg, CDialog)


//{{AFX_MSG_MAP(CHttpRequestDlg)


ON_WM_PAINT()


ON_WM_QUERYDRAGICON()


ON_BN_CLICKED(IDC_START_MSG, OnStartMsg)


//}}AFX_MSG_MAP


END_MESSAGE_MAP()






/**//////////////////////////////////////////////////////////////////////////////


// CHttpRequestDlg message handlers




BOOL CHttpRequestDlg::OnInitDialog()




...{


CDialog::OnInitDialog();




// Set the icon for this dialog. The framework does this automatically


// 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("http://news.szren.com/shenzhen/Index.html");


m_hrefliststart.SetWindowText(" <td class=fontindex14px 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_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 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); // 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.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()




...{


// 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=false;


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




...{


CInternetSession mySession(NULL,0);


CHttpFile* myHttpFile=NULL;


CString newsdata;


m_httpaddr.GetWindowText(newsdata);


myHttpFile=(CHttpFile*)mySession.OpenURL(newsdata);//打开连接






CString myData;


newsdata = "";


while(myHttpFile->ReadString(myData))//循环读文件信息




...{


newsdata=newsdata+" ";


newsdata+=myData;


}


myHttpFile->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 = 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_hrefend.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++)...{


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* myHttpFile=NULL;


myHttpFile=(CHttpFile*)mySession.OpenURL(NewsUrl);






CString myData;


CString newsdata = "";


while(myHttpFile->ReadString(myData))




...{


newsdata=newsdata+" ";


newsdata+=myData;


}


myHttpFile->Close() ;


mySession.Close() ;




PrintNewsCon(newsdata);//分析一个新闻内容//////////////


}


//分析一个新闻内容//////////////


void CHttpRequestDlg::PrintNewsCon(CString NewsUrl)




...{


CString str=NewsUrl,strtemp,strnew,msg;




int istart=0,iend=0;




/**/////////////////////////////////////////获取标题


m_titlestart.GetWindowText(strtemp);


istart = str.Find(strtemp);


istart = istart + strtemp.GetLength();


m_titleend.GetWindowText(strtemp);


iend = str.Find(strtemp);


strnew = str.Mid(istart,iend-istart);




/**/////////////////////////////////////////


m_msg.GetWindowText(msg);


msg = msg + strnew + " ";


m_msg.SetWindowText(msg);






/**/////////////////////////////////////////获取内容


m_constart.GetWindowText(strtemp);


istart = str.Find(strtemp);


istart = istart + strtemp.GetLength();


m_conend.GetWindowText(strtemp);


iend = str.Find(strtemp);


strnew = str.Mid(istart,iend-istart);




/**/////////////////////////////////////////


m_msg.GetWindowText(msg);


msg = msg + strnew + "</p></p>" + " ";


m_msg.SetWindowText(msg);




/**/////////////////////////////////////////




}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐