45
|
1 // Filename : net.h
|
|
2 // Last Change: 2020-03-30 ŒŽ 15:05:58.
|
|
3 //
|
|
4 #pragma once
|
|
5
|
|
6 #include <wx/sstream.h>
|
|
7 #include <wx/wfstream.h>
|
|
8 #include <wx/zstream.h>
|
|
9 #include <wx/tarstrm.h>
|
|
10 #include <wx/protocol/http.h>
|
|
11
|
|
12 wxString HttpGetText(wxString addr, wxString port, wxString url)
|
|
13 {
|
|
14 wxHTTP get;
|
|
15 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
|
|
16 while (!get.Connect(addr, wxAtoi(port)))
|
|
17 wxSleep(1);
|
|
18
|
|
19 wxString res;
|
|
20 wxInputStream *httpStream = get.GetInputStream(url);
|
|
21 if (get.GetError() == wxPROTO_NOERR) {
|
|
22 wxStringOutputStream out_stream(&res);
|
|
23 httpStream->Read(out_stream);
|
|
24 }
|
|
25
|
|
26 wxDELETE(httpStream);
|
|
27 get.Close();
|
|
28
|
|
29 return res;
|
|
30 };
|
|
31
|
|
32 bool HttpGetFile(wxString addr, wxString port, wxString url, wxString file)
|
|
33 {
|
|
34 bool ret = false;
|
|
35 wxHTTP get;
|
|
36 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
|
|
37 while (!get.Connect(addr, wxAtoi(port)))
|
|
38 wxSleep(1);
|
|
39
|
|
40 wxInputStream *httpStream = get.GetInputStream(url);
|
|
41 if (get.GetError() == wxPROTO_NOERR) {
|
|
42 wxFileOutputStream out_stream(file);
|
|
43 httpStream->Read(out_stream);
|
|
44 ret = true;
|
|
45 }
|
|
46
|
|
47 wxDELETE(httpStream);
|
|
48 get.Close();
|
|
49 return ret;
|
|
50 };
|
|
51
|
|
52 bool HttpGetTgzFile(wxString addr, wxString port, wxString url, wxString dir)
|
|
53 {
|
|
54 bool ret = false;
|
|
55 wxHTTP get;
|
|
56 get.SetTimeout(30);
|
|
57 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
|
|
58 while (!get.Connect(addr, wxAtoi(port)))
|
|
59 wxSleep(1);
|
|
60
|
|
61 wxInputStream *httpStream = get.GetInputStream(url);
|
|
62 if (get.GetError() == wxPROTO_NOERR) {
|
|
63 //int size = httpStream->GetSize();
|
|
64 wxZlibInputStream zlib_istream(httpStream);
|
|
65
|
|
66 wxTarEntry* entry;
|
|
67 wxTarInputStream tar_istream(zlib_istream);
|
|
68 int i = 1;
|
|
69 while ((entry = tar_istream.GetNextEntry()) != NULL) {
|
|
70 //wxString name = entry->GetName();
|
|
71 wxFileOutputStream file_ostream(wxString::Format(wxT("%s/%d"), dir, i++));
|
|
72 file_ostream.Write(tar_istream);
|
|
73 file_ostream.Close();
|
|
74 }
|
|
75 ret = true;
|
|
76 }
|
|
77
|
|
78 //wxDELETE(httpStream);
|
|
79 get.Close();
|
|
80 return ret;
|
|
81 };
|
|
82
|