-
Notifications
You must be signed in to change notification settings - Fork 0
/
Net.cpp
78 lines (67 loc) · 2.09 KB
/
Net.cpp
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
#include "stdafx.h"
#include "XmlRpc.h"
#include "Net.h"
HINTERNET Net::hInternet;
Net::Net(void) {}
void Net::initializeWininet()
{
if (hInternet == NULL) hInternet = InternetOpen(L"UltimateSubtitles", NULL, NULL, NULL, NULL);
}
HINTERNET Net::connect(const char* url)
{
initializeWininet();
return InternetConnectA(hInternet, url, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
}
void Net::disconnect(HINTERNET hConnect)
{
InternetCloseHandle(hConnect);
}
void Net::sendXmlRpc(HINTERNET hConnect, const char* object, XmlRpc& args, XmlRpc& result)
{
const char* acceptTypes[2] = { "text/*", NULL };
int flags = INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;
std::string argsXml;
args.toStr(argsXml);
HINTERNET hRequest = HttpOpenRequestA(hConnect, "POST", object, NULL, NULL, acceptTypes, flags, NULL);
// Callback
char header[255];
sprintf_s(header, "Content-Type: text/xml\r\nContent-length: %d", argsXml.size());
BOOL a = HttpAddRequestHeadersA(hRequest, header, strlen(header), HTTP_ADDREQ_FLAG_ADD);
BOOL s = HttpSendRequest(hRequest, NULL, 0, (LPVOID)argsXml.c_str(), argsXml.size());
DWORD e = GetLastError();
std::string buff;
readFile(hRequest, buff);
//print("hConnect : %u hRequest : %u add : %d send : %u %s", hConnect, hRequest, a, e, buff.c_str());
result.load(buff);
InternetCloseHandle(hRequest);
}
int Net::download(std::string url, std::string* buffer)
{
int size = 0;
initializeWininet();
HINTERNET hFile = InternetOpenUrlA(hInternet, url.c_str(), NULL, NULL, NULL, NULL);
size = readFile(hFile, *buffer);
InternetCloseHandle(hFile);
return size;
}
int Net::download(std::string url, std::wstring* buffer)
{
int size = 0;
std::string tmpBuf;
size = download(url, &tmpBuf);
buffer->append(CA2W(tmpBuf.c_str(), CP_UTF8));
return size;
}
int Net::readFile(HINTERNET hFile, std::string& buffer)
{
int size = 0;
char tmp[1024];
DWORD lengthReaded = 0;
while (InternetReadFile(hFile, tmp, 1024, &lengthReaded) == TRUE && lengthReaded > 0)
{
buffer.append(tmp, lengthReaded);
size += lengthReaded;
}
return size;
}
Net::~Net(void) {}