forked from JelinYao/HttpInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIHttpInterface.h
More file actions
143 lines (119 loc) · 3.36 KB
/
Copy pathIHttpInterface.h
File metadata and controls
143 lines (119 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*****************************************************************
*HTTP处理类,主要用于HTTP GET/POST、下载(支持重定向)功能
*Author: JelinYao
*Date: 2015/2/14 12:11
*Email: mailto://[email protected]
*/
/*****************************************************************
*/
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <string>
using std::string;
using std::wstring;
enum HttpRequest
{
Hr_Post,
Hr_Get,
};
//枚举下载状态
enum DownloadState
{
DS_Loading = 0,
DS_Fialed,
DS_Finished,
};
/******************************************************
*定义错误信息
*
******************************************************/
enum HttpInterfaceError
{
Hir_Success = 0, //成功
Hir_InitErr, //初始化失败
Hir_ConnectErr, //连接HTTP服务器失败
Hir_SendErr, //发送请求失败
Hir_QueryErr, //查询HTTP请求头失败
Hir_404, //页面不存在
Hir_IllegalUrl, //无效的URL
Hir_CreateFileErr, //创建文件失败
Hir_DownloadErr, //下载失败
Hir_QueryIPErr, //获取域名对应的地址失败
Hir_SocketErr, //套接字错误
Hir_UserCancel, //用户取消下载
Hir_BufferErr, //文件太大,缓冲区不足
Hir_HeaderErr, //HTTP请求头错误
Hir_UnknowErr,
};
//下载的回调
class IHttpCallback
{
public:
virtual void OnDownloadCallback(void* pParam, DownloadState state, double nTotalSize, double nLoadSize) = 0;
virtual bool IsNeedStop() = 0;
};
class IHttpBase
{
public:
virtual void SetDownloadCallback(IHttpCallback* pCallback, void* pParam) = 0;
virtual bool DownloadFile(LPCWSTR lpUrl, LPCWSTR lpFilePath) = 0;
virtual bool DownloadToMem(LPCWSTR lpUrl, OUT void** ppBuffer, OUT int* nSize) = 0;
virtual void FreeInstance() = 0;
virtual HttpInterfaceError GetErrorCode() = 0;
};
////////////////////////////////////////////////////////////////////////////////////
//HTTP请求接口类
class IWininetHttp
:public IHttpBase
{
public:
//HTTP请求功能
virtual string Request(LPCSTR lpUrl, HttpRequest type, LPCSTR lpPostData = NULL, LPCSTR lpHeader = NULL) = 0;
virtual string Request(LPCWSTR lpUrl, HttpRequest type, LPCSTR lpPostData = NULL, LPCWSTR lpHeader = NULL) = 0;
virtual bool DownloadFile(LPCSTR pUrl, LPCSTR pSavePath) = 0;
};
///////////////////////////////////////////////////////////////////////////////////////
//HTTP socket类
class ISocketHttp
:public IHttpBase
{
public:
virtual LPCWSTR GetIpAddr()const = 0;
};
///////////////////////////////////////////////////////////////////////////////////////
//WinHttp类
class IWinHttp
: public IHttpBase
{
public:
virtual void SetTimeOut(int dwConnectTime, int dwSendTime, int dwRecvTime) = 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
/***********************************************************
*声明导出函数部分
*
************************************************************
*/
enum HttpFlag
{
Hf_Socket = 0,
Hf_WinInet,
Hf_WinHttp,
};
LIB_FUN bool CreateInstance(IHttpBase** pBase, HttpFlag flag);