-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmHttpSocket.cpp
More file actions
269 lines (255 loc) · 6.44 KB
/
AmHttpSocket.cpp
File metadata and controls
269 lines (255 loc) · 6.44 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "StdAfx.h"
#include "AmHttpSocket.h"
#include <atlbase.h>
#include <limits.h>
#include <afxinet.h>
#define AgentName _T("Nimo Software HTTP Retriever 1.0")
//case insensitive search functions...
#ifdef UNICODE
#define _tcsustr wcsustr
#else
#define _tcsustr strustr
#endif
char* strustr(char *source, char *s);
wchar_t* wcsustr(wchar_t *source, wchar_t *s);
char* strustr(char *source, char *s)
{
//make an uppercase copy af source and s
char *csource = strdup(source);
char *cs = strdup(s);
strupr(csource);
strupr(cs);
//find cs in csource...
char *result = strstr(csource, cs);
if (result != NULL)
{
//cs is somewhere in csource
int pos = result - csource;
result = source;
result += pos;
}
//clean up
free(csource);
free(cs);
return result;
}
wchar_t* wcsustr(wchar_t *source, wchar_t *s)
{
//make an uppercase copy af source and s
wchar_t *csource = wcsdup(source);
wchar_t *cs = wcsdup(s);
wcsupr(csource);
wcsupr(cs);
//find cs in csource...
wchar_t *result = wcsstr(csource, cs);
if (result != NULL)
{
//cs is somewhere in csource
int pos = result - csource;
result = source;
result += pos;
}
//clean up
free(csource);
free(cs);
return result;
}
CAmHttpSocket::CAmHttpSocket()
{
LastError = 0;
ReceivedData = NULL;
Headers = NULL;
hIO = NULL;
hIS = NULL;
hCO = NULL;
hIO = InternetOpen(AgentName, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
}
CAmHttpSocket::~CAmHttpSocket()
{
if (ReceivedData != NULL) free(ReceivedData);
if (Headers != NULL) free(Headers);
if (hIO != NULL) InternetCloseHandle(hIO);
if (hIS != NULL) InternetCloseHandle(hIS);
if (hCO != NULL) InternetCloseHandle(hCO);
}
bool CAmHttpSocket::OpenUrl(const TCHAR *url)
{
if (hIS != NULL) InternetCloseHandle(hIS);
try
{
hIS = InternetOpenUrl(hIO, url, NULL, 0, HTTP_QUERY_DATE, 0);
}
catch(CInternetException* pEx)
{
// 捕捉WinINet异常
TCHAR szErr[1024];
if (pEx->GetErrorMessage(szErr, 1024))
{
AfxMessageBox(szErr, MB_OK);
}
else
{
AfxMessageBox(_T("无法与服务器建立连接!"), MB_OK);
}
pEx->Delete();
}
if (hIS != NULL) return true;
else
{
LastError = GetLastError();
return false;
}
}
bool CAmHttpSocket::PostUrl(const TCHAR *url, const char *PostData, int PostDataLength)
{
//check length of postdata
if (PostDataLength == -1)
PostDataLength = strlen(PostData);
//some variable that we need...
URL_COMPONENTS uc;
//let's split the url...
uc.dwStructSize = sizeof(uc);
uc.lpszScheme = NULL;
uc.dwSchemeLength = 0;
uc.lpszHostName = NULL;
uc.dwHostNameLength = 1;
uc.nPort = 0;
uc.lpszUserName = NULL;
uc.dwUserNameLength = 0;
uc.lpszPassword = NULL;
uc.dwPasswordLength = 0;
uc.lpszUrlPath = NULL;
uc.dwUrlPathLength = 1;
uc.lpszExtraInfo = NULL;
uc.dwExtraInfoLength = 0;
InternetCrackUrl(url, _tcslen(url), 0, &uc);
//post the data...
if (hCO != NULL) InternetCloseHandle(hCO);
TCHAR *HostName = _tcsdup(uc.lpszHostName);
HostName[uc.dwHostNameLength] = '\0';
TCHAR *FileName = _tcsdup(uc.lpszUrlPath);
FileName[uc.dwUrlPathLength] = '\0';
if (hIS != NULL) InternetCloseHandle(hIS); //if open, close the handle to the connection
DWORD flags;
if (uc.nPort == 80)
{
//we are talking plain http
flags = INTERNET_FLAG_NO_CACHE_WRITE;
}
else
{
//we are talking secure https
flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
}
TCHAR headers[] = _T("Content-Type: application/x-www-form-urlencoded"); //content type for post...
TCHAR szAccept[] = _T("*/*"); //we accept everything...
LPTSTR AcceptTypes[2]={0};
AcceptTypes[0]=szAccept;
hCO = InternetConnect(hIO, HostName, uc.nPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
hIS = HttpOpenRequest(hCO, _T("POST"), FileName, NULL, NULL, (LPCTSTR*)AcceptTypes, flags, 0);
if (!HttpSendRequest(hIS, headers, _tcslen(headers), (TCHAR*)PostData, PostDataLength))
{
LastError = GetLastError();
free(HostName);
free(FileName);
return false;
}
free(HostName);
free(FileName);
return true;
}
TCHAR* CAmHttpSocket::GetHeaders(const TCHAR *url)
{
//did we get an url?
if (url == NULL)
{
LastError = -1;
return NULL;
}
//open the url...
OpenUrl(url);
//delete old headers...
if (Headers != NULL) free(Headers);
Headers = (TCHAR*)calloc(1, sizeof(TCHAR));
//get the size headers
DWORD d = 1, d2 = 0;
int i = HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2);
//alloc some space for the headers
Headers = (TCHAR*)realloc(Headers, d * sizeof(TCHAR));
if (!HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2)) return NULL;
return Headers;
}
char* CAmHttpSocket::GetPage(const TCHAR *url, bool Post, const char *PostData, int PostDataLength)
{
//did we get an url?
if (url == NULL)
{
LastError = -1;
return NULL;
}
//get the page and store it in ReceivedData...
if (Post)
{
//use http post...
if (!PostUrl(url, PostData, PostDataLength)) return NULL;
}
else
{
//use http get
if (!OpenUrl(url)) return NULL;
}
const int rdsize = 8192;
char mr[rdsize];
DWORD rd;
int curpos = 0;
if (ReceivedData != NULL) free(ReceivedData);
ReceivedData = (char*)calloc(rdsize + 1, sizeof(char));
while (InternetReadFile(hIS, mr, rdsize - 1, &rd))
{
if (rd == 0) break;
mr[rd] = '\0';
curpos += rd;
ReceivedData[curpos] = '\0';
strcat(ReceivedData, mr);
ReceivedData = (char*)realloc(ReceivedData, curpos + rdsize);
}
return ReceivedData;
}
TCHAR* CAmHttpSocket::GetHeaderLine(TCHAR *s)
{
//find a line in the headers that contains s, and return a pointer to the line...
if (Headers == NULL) return NULL;
TCHAR *ts = Headers;
if (_tcsustr(ts, s) != NULL) return ts;
while (1)
{
if (*ts == '\0' && ts[1] == '\0') break;
if (*ts == '\0')
{
ts++;
if (_tcsustr(ts, s) != NULL) return ts;
}
else ts++;
}
return NULL;
}
int CAmHttpSocket::GetPageStatusCode()
{
//get the correct header line
TCHAR *s = GetHeaderLine(_T("http"));
if (s == NULL) return 0; //no headers
//find the 3 digit code...
if (_tcslen(s) < 3) return 0; //some error, the string is too short...
while (!(isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2])))
{
if (s[3] == '\0') return 0; //we have reached the end of the string, without finding the number...
s++;
}
//make a copy of s, and return the code
TCHAR *code = _tcsdup(s);
code[3] = '\0'; //remove all text after the 3 digit response code
int result = _ttoi(code);
free(code);
return result;
}