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

来源:互联网 发布:掌上电力提示网络问题 编辑:程序博客网 时间:2024/06/06 15:53

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 stringRequest(const string& strUrl, REQUEST_TYPE type, const string& strPostData="", string strHeader="")=0;virtual stringRequest(const wstring& strUrl, REQUEST_TYPE type, const wstring& strPostData=L"", wstring strHeader=L"")=0;virtual boolDownload(const wstring& strUrl, const wstring& strSavePath)=0;virtual boolDownload(const string& strUrl, const string& strSavePath)=0;// 下载消息设置virtual voidSetWnd(HWND hWnd)=0;virtual voidSetMsg(const UINT msg)=0;//出错信息获取virtual constwstring&GetErrorMsg()const=0;virtual constwchar_t*GetErrorBuffer()const=0;//转码功能virtual wstring StringToWstring(const string& str)=0;virtual stringWstringToString(const wstring& str)=0;virtual wstring Utf8ToUnicode(const string& strUtf8)=0;//释放virtual voidFreeInstance()=0;};/////////////////////////////////////////////////////////////////////////////////////////HTTP socket类enum DownloadState{DS_Loading=0,DS_Fialed,DS_Finished,};class IHttpInterface2;//下载的回调class CDownloadCallback{public:virtual voidOnDownloadCallback(IHttpInterface2* pHttpSocket, DownloadState state, int nTotalSize, int nLoadSize)=0;virtual boolIsNeedStop()=0;};class IHttpInterface2{public:virtual wstringGetIpAddr()const=0;virtual wstring GetLastError()const=0;virtual voidSetCallback(CDownloadCallback* pCallback)=0;virtual boolDownloadFile(const wstring& strUrl, const wstring& strSavePath)=0;virtual voidFreeInstance()=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#endifLIB_FUNIHttpInterface* 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_FUNIHttpInterface2* 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欢迎指正。

7 0
原创粉丝点击