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

📄 httpdownloadsocket.cpp

📁 wisock应用队列思想实现单线程多任务http下载的类. 实现多文件同时下载,断点续传,流量统计等.
💻 CPP
字号:
#include ".\httpdownloadsocket.h"
#include <TCHAR.H>
#include <wchar.h>
#include "struct.h"
CHttpDownloadSocket::CHttpDownloadSocket(void)
{
}

CHttpDownloadSocket::~CHttpDownloadSocket(void)
{
}

int CHttpDownloadSocket::SendRequest(LPTSTR buf/*buf is fileName here*/,UINT begin,UINT end)
{
	TCHAR szHeader[10240];
	int index = 0;
	_stprintf(szHeader,"%s %s%s %s\r\n","GET",m_lpszUrl ,buf,"HTTP/1.1");//用GET方法,请求的内容

	index = _tcsclen(szHeader);
	printf("%d\n",index);
	_stprintf(szHeader+index,"%s%s\r\n","Accept:","*/*");//接收所有的数据类型

	index = _tcsclen(szHeader);
	printf("%d\n",index);
	_stprintf(szHeader+index,"%s%s\r\n","User-Agent:","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)");//指定浏览器的类型

	index = _tcsclen(szHeader);
	printf("%d\n",index);
	_stprintf(szHeader+index,"%s%s:%d\r\n","host:",m_lpszHost,m_port);//主机名

	index = _tcsclen(szHeader);
	printf("%d\n",index);
	_stprintf(szHeader+index,"%s%d-\r\n","Range:bytes=",begin);//断点序传

	index = _tcsclen(szHeader);
	printf("%d\n",index);
	_stprintf(szHeader+index,"%s%s\r\n\r\n","Connection:","close");//设定为不保持连接(允许客户端或服务器中任何一方关闭底层的连接)
	


	printf("\n%dcontent:%s\n",strlen(szHeader),szHeader);

	return Send(szHeader);
}
int CHttpDownloadSocket::RecvData(LPTSTR buf)
{
	return Recv(buf,RECV_BUF_SIZE);
}
void CHttpDownloadSocket::CloseSocket()
{
	closesocket(m_socket);
}

void CHttpDownloadSocket::SetOffset(int offset)
{
	//NULL now
}

void CHttpDownloadSocket::Login()
{
	//NULL now
}

UINT CHttpDownloadSocket::GetContentLen(LPCTSTR buf)
{
	LPTSTR begin,end;//表示长度子串的起始位置
	TCHAR ContentLen[32];
	begin = _tcsstr(buf,"Content-Length") + strlen("Content-Length: "); //
	end = _tcsstr(begin,"\r\n") ;
	
	memcpy(ContentLen,begin,end-begin);
	ContentLen[end-begin] = 0;

	return _tstoi(ContentLen);
}

UINT CHttpDownloadSocket::GetHeaderLen(LPCTSTR buf)
{
	return _tcsstr(buf,"\r\n\r\n")+4 - buf ;
}

void CHttpDownloadSocket::IgnoreIpHeader(LPTSTR buf)
{
	UINT headerLen = GetHeaderLen(buf);
	UINT contentLen = GetContentLen(buf);
	TCHAR temp[RECV_BUF_SIZE];

	memcpy(buf ,buf + headerLen,RECV_BUF_SIZE - headerLen);
//	memcpy(buf,temp,contentLen);

	//for(int i = 0; i<contentLen ; i++)
	//{
	//	*(buf+i) = *(buf +headerLen+ i);
	//}
}
RESPONSECODE CHttpDownloadSocket::GetResponse(LPCTSTR buf)
{
	LPTSTR begin,end;//表示返回代号的起始位置
	TCHAR szCode[32];
	begin = _tcsstr(buf," ") + 2 ;//
	end = _tcsstr(begin," ") ;

	memcpy(szCode,begin,end - begin);

	UINT iCode = _ttoi(szCode);

	if(iCode<300)
		return CODE_OK;

	if((iCode>=300)&&(iCode<400))//重定向,目前无意义
		return CODE_ERROR;

	if((iCode>=400)&&(iCode<500))//客户端错误
		return CODE_ERROR;

	if(iCode>=500)//服务器错误,可以重试,目前不实现
		return CODE_ERROR;

	

}

⌨️ 快捷键说明

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