开源一个C++实现的简单HTTP协议处理库

HTTP协议库有两个版本,一个是基于WININET,一个则是使用socket实现的。

可以支持POST 、GET请求,断点下载、重定向下载、下载进度回调,不支持HTTPS。

接口头文件声明如下:

#pragma once
#include <string>
using std::string;
using std::wstring;
#include <stdio.h>
#include <tchar.h>



enum REQUEST_TYPE
{
	post,
	get,
};



//HTTP请求接口类
class IHttpInterface
{
public:
	//HTTP请求功能
	virtual string	Request(const string& strUrl, REQUEST_TYPE type, const string& strPostData="", string strHeader="")=0;
	virtual string	Request(const wstring& strUrl, REQUEST_TYPE type, const wstring& strPostData=L"", wstring strHeader=L"")=0;
	virtual bool	Download(const wstring& strUrl, const wstring& strSavePath)=0;
	virtual bool	Download(const string& strUrl, const string& strSavePath)=0;
	// 下载消息设置
	virtual void	SetWnd(HWND hWnd)=0;
	virtual void	SetMsg(const UINT msg)=0;
	//出错信息获取
	virtual const	wstring&	GetErrorMsg()const=0;
	virtual const	wchar_t*	GetErrorBuffer()const=0;
	//转码功能
	virtual wstring StringToWstring(const string& str)=0;
	virtual string	WstringToString(const wstring& str)=0;
	virtual wstring Utf8ToUnicode(const string& strUtf8)=0;
	//释放
	virtual void	FreeInstance()=0;
};


///
//HTTP socket类

enum DownloadState
{
	DS_Loading=0,
	DS_Fialed,
	DS_Finished,
};

class IHttpInterface2;
//下载的回调
class CDownloadCallback
{
public:
	virtual void	OnDownloadCallback(IHttpInterface2* pHttpSocket, DownloadState state, int nTotalSize, int nLoadSize)=0;
	virtual bool	IsNeedStop()=0;
};

class IHttpInterface2
{
public:
	virtual wstring	GetIpAddr()const=0;
	virtual wstring GetLastError()const=0;
	virtual void	SetCallback(CDownloadCallback* pCallback)=0;
	virtual bool	DownloadFile(const wstring& strUrl, const wstring& strSavePath)=0;
	virtual void	FreeInstance()=0;
};




/
//DLL的导出函数声明
//#define LIB_DLL
#ifdef EXPORT_LIB//导出库
	#ifdef LIB_DLL
		#define LIB_FUN extern "C" __declspec(dllexport)
	#else
		#define LIB_FUN
	#endif
#else//引用库
	#ifdef LID_DLL
		#define LIB_FUN extern "C" __declspec(dllimport)
	#else
		#define LIB_FUN
	#endif
#endif


LIB_FUN	IHttpInterface* CreateHttpInstance();//创建接口实例对象

LIB_FUN bool UrlDownload( const wstring& strUrl, const wstring& strSavePath, OUT UINT& uLoadSize, \
					  OUT wstring& strErrorMsg, HWND hWnd=NULL, UINT uMsg=0 );//提供C下载函数

LIB_FUN	IHttpInterface2* CreateHttpInstance2();

源代码使用VS2008开发,有DLL、LIB对应的编译方式。

使用时也比较简单的,示例代码如下:

// UseHttp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "../IHttp/IHttpInterface.h"
#pragma comment(lib, "../bin/Release_s/IHttp")


int _tmain(int argc, _TCHAR* argv[])
{

	IHttpInterface* pHttp=CreateHttpInstance();
	//pHttp->Download(L"http://android.shoujids.com/software/download?id=154103", L"c:\\test.apk");	
	string strUrl="http://www.baidu.com";	
	string str=pHttp->Request(strUrl, get);
	pHttp->FreeInstance();
// 	CHttpSocket hs;
// 	hs.ConnectUrl("android.shoujids.com");
// 	int nLoadSize=0;
// 	hs.DownLoadFile("http://android.shoujids.com/software/download?id=154103", "c:\\test.zpk", &nLoadSize);
	return 0;
}


最新版本开源代码已更新到github:欢迎加星收藏 https://github.com/JelinYao/HttpInterface

有bug欢迎指正。

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值