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

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

接口头文件声明如下:

#pragma once

#include

using std::string;

using std::wstring;

#include

#include

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

#endif

LIB_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;

}源码下载地址:HTTP协议库

有bug欢迎指正。

原文:http://blog.csdn.net/mfcing/article/details/42743889

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐