WxHTTP
Sample example
HTTP 프로토콜을 사용하여 데이터를 획득하는 간단한 예제는 아래와 같다.
#include <wx/wx.h>
#include <wx/string.h>
#include <wx/stream.h>
#include <wx/protocol/http.h>
#define HEADER_CONTENT_TYPE "Content-type"
#define DATA_CONTENT_TYPE "text/html; charset=utf-8"
#define REQUEST_TIMEOUT 10
#define CONNECT_RETRY 5
bool requestHttpBody(const wxString & host, const short port, const wxString & path, wxOutputStream & output)
{
wxApp::IsMainLoopRunning();
wxSocketBase::Initialize();
wxHTTP http;
http.SetHeader(wxT(HEADER_CONTENT_TYPE), wxT(DATA_CONTENT_TYPE));
http.SetTimeout(REQUEST_TIMEOUT);
for (int count = 0; count < CONNECT_RETRY; count++) {
if (http.Connect(host, port) == true) {
break;
}
// wxSleep(5);
}
bool result = false;
if (http.GetError() == wxPROTO_NOERR) {
wxInputStream * input = http.GetInputStream(path);
input->Read(output);
wxDELETE(input);
} else {
// Unable to connect!
}
http.Close();
return result;
}
Custom Thread Access
만약 메인 스레드가 아닌, 새로운 스레드에서 접근하고자 할 경우 알래와 같이 메인 스레드에서 초기화해야 한다.
이 후, wxHTTP 객체를 커스텀 스레드에서 사용하는 것이 가능해 진다.