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
| // newsreader1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet")
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
bool ok = false;
// инициализируем WinInet
HINTERNET hInternet =
::InternetOpen(
TEXT("WinInet Test"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,NULL,
0);
if (hInternet != NULL) {
// открываем HTTP сессию
HINTERNET hConnect =
::InternetConnect(
hInternet,
TEXT("www.translate.yandex.net"),
INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,
INTERNET_SERVICE_HTTP,
0,
1u);
if (hConnect != NULL) {
// открываем запрос
HINTERNET hRequest =
::HttpOpenRequest(
hConnect,
TEXT("GET"),
TEXT("/api/v1.5/tr/translate?key=trnsl.1.1.20160408T001539Z.39733f4c93734886.135cd39cbce638caeb21cc90354a9b61dd1e67b3&text=Afanasenko&lang=rus"),
NULL,
NULL,
0,
INTERNET_FLAG_KEEP_CONNECTION,
1);
if (hRequest != NULL) {
// посылаем запрос
BOOL bSend = ::HttpSendRequest(hRequest, NULL,0, NULL,0);
if (bSend) {
// создаём выходной файл
ofstream fnews("news.txt",ios::out|ios::binary);
if (fnews.is_open()) for (;;) {
// читаем данные
char szData[1024];
DWORD dwBytesRead;
BOOL bRead =
::InternetReadFile(
hRequest,
szData,sizeof(szData)-1,
&dwBytesRead);
// выход из цикла при ошибке или завершении
if (bRead == FALSE || dwBytesRead == 0)
break;
// сохраняем результат
szData[dwBytesRead] = 0;
fnews << szData;
ok = true;
}
}
// закрываем запрос
::InternetCloseHandle(hRequest);
}
// закрываем сессию
::InternetCloseHandle(hConnect);
}
// закрываем WinInet
::InternetCloseHandle(hInternet);
}
// запускаем для полного счастья считанную страничку
if (ok)
system("start news.txt");
return 0;
} |