📄 httpthread.cpp
字号:
// HttpThread.cpp: implementation of the CHttpThread class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HttpEx.h"
#include "HttpThread.h"
#include <wininet.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHttpThread::CHttpThread(): m_hPostMsgWnd(NULL),
lpszBuf(NULL)
{
}
CHttpThread::~CHttpThread()
{
if(lpszBuf)
delete lpszBuf;
}
void CHttpThread::InitHttp(HWND hPostMsgWnd)
{
m_hPostMsgWnd = hPostMsgWnd ;
}
char*CHttpThread:: GetBuffer()
{
return lpszBuf;
}
void CHttpThread::GetHttpFile(CString& strAddress)
{
CString strTemp=strAddress.Mid(7);
//检查主机的路径
int nSlash=strTemp.Find("/");
if (nSlash!=-1) //如等于-1,就是没找到
{
m_strServer=strTemp.Left(nSlash);//取‘/’左边的服务器地址
m_strPath=strTemp.Mid(nSlash);
}
else
{
m_strServer=strTemp;
}
AfxBeginThread(GetHttpFileWorkerThread, this);
}
UINT CHttpThread::GetHttpFileWorkerThread(LPVOID pvThread)
{
CHttpThread *pthread=(CHttpThread *)pvThread;
if(pthread==NULL
||pthread->m_strServer.IsEmpty()
||pthread->m_hPostMsgWnd==NULL)
{
return 0;
}
pthread->_GetHttpFile();
return 1;
}
UINT CHttpThread::_GetHttpFile()
{
//第一步:初始化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 0;
}
//第二步:初始化HTTP session
HINTERNET hConnect=::InternetConnect(hSession,//当前internet会话句柄
m_strServer,//server name
INTERNET_INVALID_PORT_NUMBER,
"",//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 0;
}
//第三步:打开一个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 0;
}
//显示等待光标
CWaitCursor wait;
//第四步:发出请求
BOOL bSendRequest=::HttpSendRequest(hHttpFile,
NULL,
0,
0,
0);
if(bSendRequest)
{
//得到文件的大小
char achQueryBuf[32];
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=5*1024;
}
//分配一个缓冲区给文件数据
lpszBuf=NULL;
lpszBuf=new char[dwFileSize+1];
//读文件
DWORD dwBytesRead=0;
BOOL bRead=::InternetReadFile(hHttpFile,
lpszBuf,
dwFileSize+1,
&dwBytesRead);
if(bRead)
{
lpszBuf[dwBytesRead]=0;
}
// 关闭INTERNET句柄
VERIFY(::InternetCloseHandle(hHttpFile));
VERIFY(::InternetCloseHandle(hConnect));
VERIFY(::InternetCloseHandle(hSession));
PostMessage(m_hPostMsgWnd,WM_READFILECOMPLETED,NULL,NULL);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -