2018-10-15 16:28:07 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/webrequest.cpp
|
|
|
|
// Purpose: wxWebRequest Sample
|
|
|
|
// Author: Tobias Taschner
|
|
|
|
// Created: 2018-10-15
|
|
|
|
// Copyright: (c) 2018 wxWidgets development team
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-18 17:19:17 -04:00
|
|
|
#include "wx/notebook.h"
|
|
|
|
#include "wx/artprov.h"
|
|
|
|
#include "wx/webrequest.h"
|
2018-10-15 16:28:07 -04:00
|
|
|
|
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class WebRequestFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
2018-10-21 17:03:01 -04:00
|
|
|
enum Pages
|
|
|
|
{
|
|
|
|
Page_Image,
|
|
|
|
Page_Text,
|
|
|
|
Page_Download,
|
|
|
|
Page_Advanced
|
|
|
|
};
|
|
|
|
|
2018-10-15 16:28:07 -04:00
|
|
|
WebRequestFrame(const wxString& title):
|
|
|
|
wxFrame(NULL, wxID_ANY, title)
|
|
|
|
{
|
|
|
|
// set the frame icon
|
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
// Prepare UI controls
|
|
|
|
|
|
|
|
// If menus are not available add a button to access the about box
|
|
|
|
wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
mainSizer->Add(new wxStaticText(this, wxID_ANY, "Request URL:"),
|
2018-10-15 16:28:07 -04:00
|
|
|
wxSizerFlags().Border());
|
2018-10-21 17:03:01 -04:00
|
|
|
m_urlTextCtrl = new wxTextCtrl(this, wxID_ANY,
|
2018-10-15 16:28:07 -04:00
|
|
|
"https://www.wxwidgets.org/downloads/logos/blocks.png");
|
2018-10-21 17:03:01 -04:00
|
|
|
mainSizer->Add(m_urlTextCtrl,
|
2018-10-15 16:28:07 -04:00
|
|
|
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
m_notebook = new wxNotebook(this, wxID_ANY);
|
|
|
|
m_notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &WebRequestFrame::OnNotebookPageChanged, this);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
// Image page
|
|
|
|
wxPanel* imagePanel = new wxPanel(m_notebook);
|
|
|
|
wxSizer* imageSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
|
|
|
m_imageStaticBitmap = new wxStaticBitmap(imagePanel,
|
2018-10-15 16:28:07 -04:00
|
|
|
wxID_ANY, wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
2018-10-21 17:03:01 -04:00
|
|
|
imageSizer->Add(m_imageStaticBitmap, wxSizerFlags(1).Expand());
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
imagePanel->SetSizer(imageSizer);
|
|
|
|
m_notebook->AddPage(imagePanel, "Image", true);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
// Text page
|
|
|
|
wxPanel* textPanel = new wxPanel(m_notebook);
|
|
|
|
wxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
m_postCheckBox = new wxCheckBox(textPanel, wxID_ANY, "Post request body");
|
|
|
|
textSizer->Add(m_postCheckBox, wxSizerFlags().Border());
|
|
|
|
m_postCheckBox->Bind(wxEVT_CHECKBOX, &WebRequestFrame::OnPostCheckBox, this);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-22 15:39:30 -04:00
|
|
|
m_postRequestTextCtrl = new wxTextCtrl(textPanel, wxID_ANY,
|
|
|
|
"app=WebRequestSample&version=1",
|
|
|
|
wxDefaultPosition, wxSize(-1, FromDIP(60)), wxTE_MULTILINE);
|
2018-10-21 17:03:01 -04:00
|
|
|
textSizer->Add(m_postRequestTextCtrl,
|
2018-10-22 15:39:30 -04:00
|
|
|
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
textSizer->Add(new wxStaticText(textPanel, wxID_ANY, "Request body content type:"),
|
|
|
|
wxSizerFlags().Border());
|
|
|
|
m_postContentTypeTextCtrl = new wxTextCtrl(textPanel, wxID_ANY,
|
2018-10-22 15:39:30 -04:00
|
|
|
"application/x-www-form-urlencoded");
|
2018-10-21 17:03:01 -04:00
|
|
|
textSizer->Add(m_postContentTypeTextCtrl,
|
|
|
|
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
textSizer->Add(new wxStaticText(textPanel, wxID_ANY, "Response body:"),
|
2018-10-15 16:28:07 -04:00
|
|
|
wxSizerFlags().Border());
|
2018-10-21 17:03:01 -04:00
|
|
|
m_textResponseTextCtrl = new wxTextCtrl(textPanel, wxID_ANY, "",
|
2018-10-15 16:28:07 -04:00
|
|
|
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
|
2018-10-21 17:03:01 -04:00
|
|
|
m_textResponseTextCtrl->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
|
|
|
textSizer->Add(m_textResponseTextCtrl,
|
2018-10-15 16:28:07 -04:00
|
|
|
wxSizerFlags(1).Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM));
|
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
textPanel->SetSizer(textSizer);
|
|
|
|
m_notebook->AddPage(textPanel, "Text");
|
|
|
|
|
|
|
|
// Download page
|
|
|
|
wxPanel* downloadPanel = new wxPanel(m_notebook);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
m_notebook->AddPage(downloadPanel, "Download");
|
|
|
|
|
|
|
|
// Advanced page
|
|
|
|
wxPanel* advancedPanel = new wxPanel(m_notebook);
|
|
|
|
|
|
|
|
m_notebook->AddPage(advancedPanel, "Advanced");
|
|
|
|
|
|
|
|
mainSizer->Add(m_notebook, wxSizerFlags(1).Expand().Border());
|
|
|
|
|
|
|
|
m_startButton = new wxButton(this, wxID_ANY, "&Start Request");
|
|
|
|
m_startButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnStartButton, this);
|
|
|
|
mainSizer->Add(m_startButton, wxSizerFlags().Border());
|
|
|
|
|
|
|
|
wxCommandEvent evt;
|
|
|
|
OnPostCheckBox(evt);
|
2018-10-15 16:28:07 -04:00
|
|
|
|
|
|
|
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
|
|
|
SetSizer(mainSizer);
|
|
|
|
|
|
|
|
SetSize(FromDIP(wxSize(400, 500)));
|
|
|
|
|
|
|
|
CreateStatusBar();
|
|
|
|
}
|
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
void OnStartButton(wxCommandEvent& WXUNUSED(evt))
|
2018-10-15 16:28:07 -04:00
|
|
|
{
|
2018-10-21 17:03:01 -04:00
|
|
|
GetStatusBar()->SetStatusText("Started request...");
|
2018-10-18 17:19:17 -04:00
|
|
|
|
|
|
|
// Create request for the specified URL from the default session
|
|
|
|
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
2018-10-21 17:03:01 -04:00
|
|
|
m_urlTextCtrl->GetValue()));
|
2018-10-18 17:19:17 -04:00
|
|
|
|
2018-10-23 16:27:14 -04:00
|
|
|
// Bind event for state change
|
|
|
|
request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this);
|
2018-10-18 17:19:17 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
// Prepare request based on selected action
|
|
|
|
switch (m_notebook->GetSelection())
|
|
|
|
{
|
|
|
|
case Page_Image:
|
2018-10-22 15:39:30 -04:00
|
|
|
// Reset static bitmap image
|
|
|
|
m_imageStaticBitmap->SetBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
2018-10-21 17:03:01 -04:00
|
|
|
break;
|
|
|
|
case Page_Text:
|
2018-10-22 15:39:30 -04:00
|
|
|
// Reset response text control
|
|
|
|
m_textResponseTextCtrl->Clear();
|
|
|
|
|
|
|
|
// Set postdata if checked
|
2018-10-21 17:03:01 -04:00
|
|
|
if (m_postCheckBox->IsChecked())
|
|
|
|
{
|
|
|
|
request->SetData(m_postRequestTextCtrl->GetValue(),
|
|
|
|
m_postContentTypeTextCtrl->GetValue());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Page_Download:
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case Page_Advanced:
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_startButton->Disable();
|
|
|
|
|
2018-10-18 17:19:17 -04:00
|
|
|
// Start the request (events will be called on success or failure)
|
|
|
|
request->Start();
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:27:14 -04:00
|
|
|
void OnWebRequestState(wxWebRequestEvent& evt)
|
2018-10-21 17:03:01 -04:00
|
|
|
{
|
2018-10-23 16:27:14 -04:00
|
|
|
m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active);
|
2018-10-18 17:19:17 -04:00
|
|
|
|
2018-10-23 16:27:14 -04:00
|
|
|
switch (evt.GetState())
|
|
|
|
{
|
|
|
|
case wxWebRequest::State_Completed:
|
|
|
|
switch (m_notebook->GetSelection())
|
|
|
|
{
|
|
|
|
case Page_Image:
|
|
|
|
{
|
|
|
|
wxImage img(*evt.GetResponse()->GetStream());
|
|
|
|
m_imageStaticBitmap->SetBitmap(img);
|
|
|
|
m_notebook->GetPage(Page_Image)->Layout();
|
|
|
|
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Page_Text:
|
|
|
|
m_textResponseTextCtrl->SetValue(evt.GetResponse()->AsString());
|
|
|
|
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes text data (Status: %d %s)",
|
|
|
|
evt.GetResponse()->GetContentLength(),
|
|
|
|
evt.GetResponse()->GetStatus(),
|
|
|
|
evt.GetResponse()->GetStatusText()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case wxWebRequest::State_Failed:
|
|
|
|
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
|
|
|
GetStatusBar()->SetStatusText("");
|
|
|
|
}
|
2018-10-21 17:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
|
|
|
m_postContentTypeTextCtrl->Enable(m_postCheckBox->IsChecked());
|
|
|
|
m_postRequestTextCtrl->Enable(m_postCheckBox->IsChecked());
|
|
|
|
wxColour textBg = wxSystemSettings::GetColour(
|
|
|
|
(m_postCheckBox->IsChecked()) ? wxSYS_COLOUR_WINDOW : wxSYS_COLOUR_BTNFACE);
|
|
|
|
|
|
|
|
m_postContentTypeTextCtrl->SetBackgroundColour(textBg);
|
|
|
|
m_postRequestTextCtrl->SetBackgroundColour(textBg);
|
2018-10-15 16:28:07 -04:00
|
|
|
}
|
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
void OnNotebookPageChanged(wxBookCtrlEvent& event)
|
2018-10-15 16:28:07 -04:00
|
|
|
{
|
2018-10-21 17:03:01 -04:00
|
|
|
wxString defaultURL;
|
|
|
|
switch (event.GetSelection())
|
|
|
|
{
|
|
|
|
case Page_Image:
|
|
|
|
defaultURL = "https://www.wxwidgets.org/downloads/logos/blocks.png";
|
|
|
|
break;
|
|
|
|
case Page_Text:
|
2018-10-22 15:39:30 -04:00
|
|
|
defaultURL = "https://httpbin.org/post";
|
2018-10-21 17:03:01 -04:00
|
|
|
break;
|
|
|
|
case Page_Download:
|
|
|
|
defaultURL = "https://www.wxwidgets.com/download.zip";
|
|
|
|
break;
|
|
|
|
case Page_Advanced:
|
|
|
|
defaultURL = "https://www.wxwidgets.com/adv.zip";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_urlTextCtrl->SetValue(defaultURL);
|
2018-10-15 16:28:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-21 17:03:01 -04:00
|
|
|
wxNotebook* m_notebook;
|
|
|
|
wxTextCtrl* m_urlTextCtrl;
|
|
|
|
wxButton* m_startButton;
|
|
|
|
wxStaticBitmap* m_imageStaticBitmap;
|
2018-10-15 16:28:07 -04:00
|
|
|
|
2018-10-21 17:03:01 -04:00
|
|
|
wxCheckBox* m_postCheckBox;
|
2018-10-15 16:28:07 -04:00
|
|
|
wxTextCtrl* m_postContentTypeTextCtrl;
|
|
|
|
wxTextCtrl* m_postRequestTextCtrl;
|
2018-10-21 17:03:01 -04:00
|
|
|
wxTextCtrl* m_textResponseTextCtrl;
|
2018-10-15 16:28:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class WebRequestApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit() wxOVERRIDE
|
|
|
|
{
|
2018-10-17 05:12:19 -04:00
|
|
|
if ( !wxApp::OnInit() )
|
2018-10-15 16:28:07 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// create the main application window
|
|
|
|
WebRequestFrame *frame = new WebRequestFrame("wxWebRequest Sample App");
|
|
|
|
frame->Show(true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wxIMPLEMENT_APP(WebRequestApp);
|