forked from JelinYao/HttpInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseHttp.cpp
More file actions
159 lines (146 loc) · 3.29 KB
/
Copy pathUseHttp.cpp
File metadata and controls
159 lines (146 loc) · 3.29 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// UseHttp.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "..//IHttp/IHttpInterface.h"
#ifdef _DEBUG
#pragma comment(lib, "..//bin//Debug//IHttpD")
#else
#pragma comment(lib, "..//bin//Release//IHttp")
#endif
#include <crtdbg.h>
#include <Windows.h>
bool TestWinInet();
bool TestWinHttp();
bool TestSocketHttp();
bool TestDownloadFile();
class CMyCallback
: public IHttpCallback
{
public:
virtual void OnDownloadCallback(void* pParam, DownloadState state, double nTotalSize, double nLoadSize)
{
if (nTotalSize>0)
{
int nPercent = (int)( 100*(nLoadSize/nTotalSize) );
printf("下载进度:%d%%\n", nPercent);
}
}
virtual bool IsNeedStop()
{
//如果需要在外部终止下载,返回true
return false;//继续下载
}
};
//extern "C" __declspec(dllimport ) bool CreateInstance(IHttpBase** pBase, HttpFlag flag);
int _tmain(int argc, _TCHAR* argv[])
{
//TestWinInet(); //测试使用WinInet实现的HTTP接口
//TestWinHttp(); //测试使用WinHttp实现的HTTP接口
//TestSocketHttp(); //测试使用Socket实现的HTTP接口
TestDownloadFile(); //测试下载文件,使用回调接口获取下载进度
system("pause");
//打印出内存泄漏信息
_CrtDumpMemoryLeaks();
return 0;
}
bool TestWinInet()
{
IWininetHttp* pHttp;
bool bRet = CreateInstance((IHttpBase**)&pHttp, Hf_WinInet);
if (!bRet)
{
return false;
}
char* pMem = NULL;
int nSize = 0;
const wchar_t* pUrl = L"http://blog.csdn.net/mfcing";
string str = pHttp->Request(pUrl, Hr_Get);
if (str.empty())
{
//请求失败
pHttp->FreeInstance();
return false;
}
if (pHttp->DownloadToMem(pUrl, (void**)&pMem, &nSize))
{
//下载到内存中,与下载到本地相比效率更高,不用读写文件
//用完之后一定要释放这块内存空间
free(pMem);
}
else
{
//下载失败,获取错误信息
HttpInterfaceError code = pHttp->GetErrorCode();
pHttp->FreeInstance();
return false;
}
pHttp->FreeInstance();
return true;
}
bool TestWinHttp()
{
IWinHttp* pHttp;
bool bRet = CreateInstance((IHttpBase**)&pHttp, Hf_WinHttp);
if (!bRet)
{
return false;
}
const char* pUrl = "www.haoso.com";
string strHtml = pHttp->Request(pUrl, Hr_Get);
if (strHtml.empty())
{
//请求失败
pHttp->FreeInstance();
return false;
}
else
{
printf("%s html : %s", pUrl, strHtml.c_str());
}
pHttp->FreeInstance();
return true;
}
bool TestSocketHttp()
{
ISocketHttp* pHttp;
bool bRet = CreateInstance((IHttpBase**)&pHttp, Hf_Socket);
if (!bRet)
{
return false;
}
const wchar_t* pUrl = L"www.sogou.com";
char* pHtml = NULL;
int nSize = 0;
if (!pHttp->DownloadToMem(pUrl, (void**)&pHtml, &nSize))
{
//下载失败
pHttp->FreeInstance();
return false;
}
printf("html: %s\n", pHtml);
//释放内存空间
free(pHtml);
pHttp->FreeInstance();
return true;
}
bool TestDownloadFile()
{
IWinHttp* pHttp;
bool bRet = CreateInstance((IHttpBase**)&pHttp, Hf_WinHttp);
if (!bRet)
{
return false;
}
CMyCallback cb;
pHttp->SetDownloadCallback(&cb, NULL);
const wchar_t* pUrl = L"http://sw.bos.baidu.com/sw-search-sp/software/97e90d6bfca7b/WeChat_2.2.0.37_Setup.exe";
const wchar_t* pSavePath = L"c:\\down.exe";
if (!pHttp->DownloadFile(pUrl, pSavePath))
{
//下载失败
pHttp->FreeInstance();
return false;
}
pHttp->FreeInstance();
return true;
}