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

📄 httpdlg.cpp

📁 INTERNET网络高级编程的包括邮件加密、MAPI、ISAPI、ACTIVEX、FTP等等。
💻 CPP
字号:
// httpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "http.h"
#include "httpDlg.h"

///////必须加上/////////
#include "wininet.h"
/////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHttpDlg dialog

CHttpDlg::CHttpDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHttpDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHttpDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_strServer="";
	m_strPath="";
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHttpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHttpDlg)
	DDX_Control(pDX, IDOK, m_btnGo);
	DDX_Control(pDX, IDCANCEL, m_btnClose);
	DDX_Control(pDX, IDC_EDIT_URL, m_editUrl);
	DDX_Control(pDX, IDC_EDIT_HTML, m_editHtml);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHttpDlg, CDialog)
	//{{AFX_MSG_MAP(CHttpDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHttpDlg message handlers

BOOL CHttpDlg::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_editUrl.SetWindowText("http://wjl.scu.edu.cn/index.html");
	m_editUrl.SetFocus();
	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 CHttpDlg::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 CHttpDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHttpDlg::OnOK() 
{
	// TODO: Add extra validation here
	//得到当前地址的字符串
	CString strUrl;
	m_editUrl.GetWindowText(strUrl);
	//判断地址是否有效,如为空或开头7个字符不是“http://"
	if(strUrl.IsEmpty()||strUrl.Left(7)!="http://")
	{
		AfxMessageBox("Sorry,It is a invalid address!");
		return;
	}
	//解析地址,得到server名字和文件路径
	ParseURL(strUrl);
	//第一步:初始化internet DLL,这是第一个被调用的函数
	HINTERNET hSession=::InternetOpen("Raw HTML Reader",PRE_CONFIG_INTERNET_ACCESS,
		                              "",INTERNET_INVALID_PORT_NUMBER,0);

	//判断会话句柄是否有效
	if(hSession==NULL)
	{
		AfxMessageBox("Internet session initalization failed!");
		return;
	}
	//第二步:初始化HTTP session
	HINTERNET	hConnect=::InternetConnect(hSession,//当前internet会话句柄
		                                   m_strServer,//server name
										   INTERNET_INVALID_PORT_NUMBER,
										   NULL,//"",//user name
										   "",//password
										   INTERNET_SERVICE_HTTP,//Type of service to access
										   0,
										   0);
	//判断连接句柄是否有效
	if(hConnect==NULL)
	{
		AfxMessageBox("Internet connect initalization failed!");
		//关闭会话句柄
		VERIFY(::InternetCloseHandle(hSession));
		return;
	}
    
	//第三步:打开一个HTTP请求句柄
	HINTERNET hHttpFile=::HttpOpenRequest(hConnect,
										  "GET",
										  m_strPath,
										  HTTP_VERSION,
										  NULL,
										  0,
										  INTERNET_FLAG_DONT_CACHE,
										  0);
	//判断连接句柄是否有效
	//判断会话句柄是否有效
	if(hHttpFile==NULL)
	{
		AfxMessageBox("Http request failed!");
		VERIFY(::InternetCloseHandle(hConnect));
		VERIFY(::InternetCloseHandle(hSession));
		return;
	}

	//使用户界面无效
    m_editUrl.EnableWindow(FALSE);
	m_btnGo.EnableWindow(FALSE);
	m_btnClose.EnableWindow(FALSE);

	//显示等待光标
	CWaitCursor wait;

	//第四步:发出请求
	BOOL bSendRequest=::HttpSendRequest(hHttpFile,
									   NULL,
									   0,
									   0,
									   0);
	if(bSendRequest)
	{
		//得到文件的大小
		char achQueryBuf[16];
		DWORD dwFileSize;
		DWORD dwQueryBufLen=sizeof(achQueryBuf);
		BOOL bQuery=::HttpQueryInfo(hHttpFile,
			                        HTTP_QUERY_CONTENT_LENGTH,
									achQueryBuf,
									&dwQueryBufLen,
									NULL);
		if(bQuery)
		{
			//查找成功,指出需要存放文件的内存大小???????
            dwFileSize=(DWORD)atol(achQueryBuf);
		}
		else
		{
			//失败,猜出一个最大文件数
			dwFileSize=10*1024;
		}

		//分配一个缓冲区给文件数据
		char *lpszBuf=new char[dwFileSize+1];

		//读文件
		DWORD dwBytesRead;
		BOOL bRead=::InternetReadFile(hHttpFile,
			                          lpszBuf,
									  dwFileSize+1,
									  &dwBytesRead);

		//显示HTML的源码
		DisplayRawHtml(lpszBuf);

		//清除缓冲区
		delete lpszBuf;

		// 关闭INTERNET句柄
		VERIFY(::InternetCloseHandle(hHttpFile));
		VERIFY(::InternetCloseHandle(hConnect));
		VERIFY(::InternetCloseHandle(hSession));
	}

	//使用户界面有效
    m_editUrl.EnableWindow(TRUE);
	m_btnGo.EnableWindow(TRUE);
	m_btnClose.EnableWindow(TRUE);	

	//
}
void CHttpDlg::ParseURL(CString&strUrl)
{
	if(strUrl.IsEmpty())
	{
		return;
	}
	//去掉"http://"
	CString strTemp=strUrl.Mid(7);
	
	//检查主机的路径
	int nSlash=strTemp.Find("/");
	if (nSlash!=-1)  //如等于-1,就是没找到
	{
		m_strServer=strTemp.Left(nSlash);//取‘/’左边的服务器地址
        m_strPath=strTemp.Mid(nSlash);
	}
	else
		m_strServer=strTemp;
//	CDialog::OnOK();
}

void CHttpDlg::DisplayRawHtml(char *lpszBuffer)
{
	m_editHtml.SetWindowText((LPCTSTR)lpszBuffer);
}

⌨️ 快捷键说明

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