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

📄 httprequest.h

📁 有关于C++网络传输操作的一个DLL开发,希望对学习C++的人有帮助
💻 H
字号:
//#include "stdafx.h"
#include <fstream>
#include <string>
#include <windows.h>
#include <wininet.h>

#pragma once 

#include "log.h"

#pragma comment(lib,"version")
#pragma  comment(lib,"Wininet.lib")

using namespace std;

//读取HTTP服务器上面的文件到本地
bool getHTTPFile(const string webfile, string &strRet, const unsigned int port)
{
	
	HINTERNET hINet = ::InternetOpen("Get_File", PRE_CONFIG_INTERNET_ACCESS,
                                NULL, INTERNET_INVALID_PORT_NUMBER, 0) ;
	if (!hINet){
		Log("初始化Wininet.dll失败,可能是Wininet.dll已经损坏!");
		return false;
	}

	HINTERNET hUrlFile = ::InternetOpenUrl(hINet, webfile.c_str(), NULL,  0,  INTERNET_FLAG_RELOAD, 0) ;
	if (!hUrlFile) {
		InternetCloseHandle(hUrlFile);
		Log("连接服务器失败,请确认网络连接已经打开并且服务器已经打开!" );
		return false;
	}

	char buffer[4*1024] ={0} ;
	DWORD dwBytesRead = 0;
	BOOL bRead = ::InternetReadFile(hUrlFile, buffer, sizeof(buffer), &dwBytesRead);
	if (!bRead) {
		InternetCloseHandle(hUrlFile);
		InternetCloseHandle(hINet);
		Log("发送请求至服务器失败,请确认网络连接已经打开并且服务器已经打开!" );
		return false;
	}

	strRet = buffer;

	InternetCloseHandle(hINet);
	//the code below is determine the server has the file or not
	//it may be work,but it's too ugly and dirty,who fix it?!
	static const basic_string <char>::size_type npos = -1;//page cannot be found
	basic_string <char>::size_type ret1=strRet.find("page cannot be found");	
	basic_string <char>::size_type ret2=strRet.find("无法找到网页");

	if (ret1!=npos||ret2!=npos)		
		//if (dwTotal==4040)
	{
		Log("服务器上没有要找的文件!");
		return false;
	}

	return true;
}

⌨️ 快捷键说明

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