Expand webrequest sample
This commit is contained in:
@@ -29,6 +29,14 @@
|
|||||||
class WebRequestFrame : public wxFrame
|
class WebRequestFrame : public wxFrame
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum Pages
|
||||||
|
{
|
||||||
|
Page_Image,
|
||||||
|
Page_Text,
|
||||||
|
Page_Download,
|
||||||
|
Page_Advanced
|
||||||
|
};
|
||||||
|
|
||||||
WebRequestFrame(const wxString& title):
|
WebRequestFrame(const wxString& title):
|
||||||
wxFrame(NULL, wxID_ANY, title)
|
wxFrame(NULL, wxID_ANY, title)
|
||||||
{
|
{
|
||||||
@@ -39,72 +47,77 @@ public:
|
|||||||
|
|
||||||
// If menus are not available add a button to access the about box
|
// If menus are not available add a button to access the about box
|
||||||
wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
|
wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
|
||||||
wxNotebook* notebook = new wxNotebook(this, wxID_ANY);
|
|
||||||
|
|
||||||
// Get image page
|
mainSizer->Add(new wxStaticText(this, wxID_ANY, "Request URL:"),
|
||||||
wxPanel* getPanel = new wxPanel(notebook);
|
|
||||||
wxSizer* getSizer = new wxBoxSizer(wxVERTICAL);
|
|
||||||
getSizer->Add(new wxStaticText(getPanel, wxID_ANY, "Image URL to load:"),
|
|
||||||
wxSizerFlags().Border());
|
wxSizerFlags().Border());
|
||||||
m_getURLTextCtrl = new wxTextCtrl(getPanel, wxID_ANY,
|
m_urlTextCtrl = new wxTextCtrl(this, wxID_ANY,
|
||||||
"https://www.wxwidgets.org/downloads/logos/blocks.png");
|
"https://www.wxwidgets.org/downloads/logos/blocks.png");
|
||||||
getSizer->Add(m_getURLTextCtrl,
|
mainSizer->Add(m_urlTextCtrl,
|
||||||
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
||||||
|
|
||||||
wxButton* getLoadButton = new wxButton(getPanel, wxID_ANY, "&Load");
|
m_notebook = new wxNotebook(this, wxID_ANY);
|
||||||
getLoadButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnGetLoadButton, this);
|
m_notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &WebRequestFrame::OnNotebookPageChanged, this);
|
||||||
getSizer->Add(getLoadButton, wxSizerFlags().Border());
|
|
||||||
|
|
||||||
m_getImageBox =
|
// Image page
|
||||||
new wxStaticBoxSizer(wxVERTICAL, getPanel, "Image");
|
wxPanel* imagePanel = new wxPanel(m_notebook);
|
||||||
m_getStaticBitmap = new wxStaticBitmap(m_getImageBox->GetStaticBox(),
|
wxSizer* imageSizer = new wxBoxSizer(wxVERTICAL);
|
||||||
|
|
||||||
|
m_imageStaticBitmap = new wxStaticBitmap(imagePanel,
|
||||||
wxID_ANY, wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
wxID_ANY, wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
||||||
m_getImageBox->Add(m_getStaticBitmap, wxSizerFlags(1).Expand());
|
imageSizer->Add(m_imageStaticBitmap, wxSizerFlags(1).Expand());
|
||||||
getSizer->Add(m_getImageBox, wxSizerFlags(1).Expand().Border());
|
|
||||||
|
|
||||||
getPanel->SetSizer(getSizer);
|
imagePanel->SetSizer(imageSizer);
|
||||||
notebook->AddPage(getPanel, "GET Image", true);
|
m_notebook->AddPage(imagePanel, "Image", true);
|
||||||
|
|
||||||
// POST Text page
|
// Text page
|
||||||
wxPanel* postPanel = new wxPanel(notebook);
|
wxPanel* textPanel = new wxPanel(m_notebook);
|
||||||
wxSizer* postSizer = new wxBoxSizer(wxVERTICAL);
|
wxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
|
||||||
postSizer->Add(new wxStaticText(postPanel, wxID_ANY, "Request URL:"),
|
|
||||||
wxSizerFlags().Border());
|
|
||||||
m_postURLTextCtrl = new wxTextCtrl(postPanel, wxID_ANY,
|
|
||||||
"https://api.github.com/");
|
|
||||||
postSizer->Add(m_postURLTextCtrl,
|
|
||||||
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
|
||||||
|
|
||||||
postSizer->Add(new wxStaticText(postPanel, wxID_ANY, "Content type:"),
|
m_postCheckBox = new wxCheckBox(textPanel, wxID_ANY, "Post request body");
|
||||||
wxSizerFlags().Border());
|
textSizer->Add(m_postCheckBox, wxSizerFlags().Border());
|
||||||
m_postContentTypeTextCtrl = new wxTextCtrl(postPanel, wxID_ANY,
|
m_postCheckBox->Bind(wxEVT_CHECKBOX, &WebRequestFrame::OnPostCheckBox, this);
|
||||||
"application/json");
|
|
||||||
postSizer->Add(m_postContentTypeTextCtrl,
|
|
||||||
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
|
||||||
|
|
||||||
postSizer->Add(new wxStaticText(postPanel, wxID_ANY, "Request body:"),
|
m_postRequestTextCtrl = new wxTextCtrl(textPanel, wxID_ANY, "",
|
||||||
wxSizerFlags().Border());
|
|
||||||
m_postRequestTextCtrl = new wxTextCtrl(postPanel, wxID_ANY, "",
|
|
||||||
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
|
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
|
||||||
postSizer->Add(m_postRequestTextCtrl,
|
textSizer->Add(m_postRequestTextCtrl,
|
||||||
wxSizerFlags(1).Expand().Border(wxLEFT | wxRIGHT));
|
wxSizerFlags(1).Expand().Border(wxLEFT | wxRIGHT));
|
||||||
|
|
||||||
wxButton* postSendButton = new wxButton(postPanel, wxID_ANY, "&Send");
|
textSizer->Add(new wxStaticText(textPanel, wxID_ANY, "Request body content type:"),
|
||||||
postSendButton->Bind(wxEVT_BUTTON, &WebRequestFrame::OnPostSendButton, this);
|
|
||||||
postSizer->Add(postSendButton, wxSizerFlags().Border());
|
|
||||||
|
|
||||||
postSizer->Add(new wxStaticText(postPanel, wxID_ANY, "Response body:"),
|
|
||||||
wxSizerFlags().Border());
|
wxSizerFlags().Border());
|
||||||
m_postResponseTextCtrl = new wxTextCtrl(postPanel, wxID_ANY, "",
|
m_postContentTypeTextCtrl = new wxTextCtrl(textPanel, wxID_ANY,
|
||||||
|
"application/json");
|
||||||
|
textSizer->Add(m_postContentTypeTextCtrl,
|
||||||
|
wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT));
|
||||||
|
|
||||||
|
textSizer->Add(new wxStaticText(textPanel, wxID_ANY, "Response body:"),
|
||||||
|
wxSizerFlags().Border());
|
||||||
|
m_textResponseTextCtrl = new wxTextCtrl(textPanel, wxID_ANY, "",
|
||||||
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
|
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
|
||||||
m_postResponseTextCtrl->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
m_textResponseTextCtrl->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
||||||
postSizer->Add(m_postResponseTextCtrl,
|
textSizer->Add(m_textResponseTextCtrl,
|
||||||
wxSizerFlags(1).Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM));
|
wxSizerFlags(1).Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM));
|
||||||
|
|
||||||
postPanel->SetSizer(postSizer);
|
textPanel->SetSizer(textSizer);
|
||||||
notebook->AddPage(postPanel, "POST Text");
|
m_notebook->AddPage(textPanel, "Text");
|
||||||
|
|
||||||
mainSizer->Add(notebook, wxSizerFlags(1).Expand().Border());
|
// Download page
|
||||||
|
wxPanel* downloadPanel = new wxPanel(m_notebook);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
|
||||||
SetSizer(mainSizer);
|
SetSizer(mainSizer);
|
||||||
@@ -114,50 +127,115 @@ public:
|
|||||||
CreateStatusBar();
|
CreateStatusBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnGetLoadButton(wxCommandEvent& WXUNUSED(evt))
|
void OnStartButton(wxCommandEvent& WXUNUSED(evt))
|
||||||
{
|
{
|
||||||
GetStatusBar()->SetStatusText("Requesting image...");
|
GetStatusBar()->SetStatusText("Started request...");
|
||||||
|
|
||||||
// Create request for the specified URL from the default session
|
// Create request for the specified URL from the default session
|
||||||
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
||||||
m_getURLTextCtrl->GetValue()));
|
m_urlTextCtrl->GetValue()));
|
||||||
|
|
||||||
// Bind events for failure and success
|
// Bind event for failure
|
||||||
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnGetWebRequestReady, this);
|
|
||||||
request->Bind(wxEVT_WEBREQUEST_FAILED, &WebRequestFrame::OnWebRequestFailed, this);
|
request->Bind(wxEVT_WEBREQUEST_FAILED, &WebRequestFrame::OnWebRequestFailed, this);
|
||||||
|
|
||||||
|
// Prepare request based on selected action
|
||||||
|
switch (m_notebook->GetSelection())
|
||||||
|
{
|
||||||
|
case Page_Image:
|
||||||
|
// Bind completion event to response as image
|
||||||
|
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnImageRequestReady, this);
|
||||||
|
break;
|
||||||
|
case Page_Text:
|
||||||
|
if (m_postCheckBox->IsChecked())
|
||||||
|
{
|
||||||
|
request->SetData(m_postRequestTextCtrl->GetValue(),
|
||||||
|
m_postContentTypeTextCtrl->GetValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnTextRequestReady, this);
|
||||||
|
break;
|
||||||
|
case Page_Download:
|
||||||
|
// TODO: implement
|
||||||
|
break;
|
||||||
|
case Page_Advanced:
|
||||||
|
// TODO: implement
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_startButton->Disable();
|
||||||
|
|
||||||
// Start the request (events will be called on success or failure)
|
// Start the request (events will be called on success or failure)
|
||||||
request->Start();
|
request->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnGetWebRequestReady(wxWebRequestEvent& evt)
|
void OnImageRequestReady(wxWebRequestEvent& evt)
|
||||||
{
|
{
|
||||||
wxImage img(*evt.GetResponse()->GetStream());
|
wxImage img(*evt.GetResponse()->GetStream());
|
||||||
m_getStaticBitmap->SetBitmap(img);
|
m_imageStaticBitmap->SetBitmap(img);
|
||||||
m_getImageBox->Layout();
|
m_notebook->GetPage(Page_Image)->Layout();
|
||||||
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
|
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
|
||||||
|
m_startButton->Enable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnTextRequestReady(wxWebRequestEvent& evt)
|
||||||
|
{
|
||||||
|
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()));
|
||||||
|
m_startButton->Enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnWebRequestFailed(wxWebRequestEvent& evt)
|
void OnWebRequestFailed(wxWebRequestEvent& evt)
|
||||||
{
|
{
|
||||||
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
||||||
GetStatusBar()->SetStatusText("");
|
GetStatusBar()->SetStatusText("");
|
||||||
|
m_startButton->Enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnPostSendButton(wxCommandEvent& WXUNUSED(evt))
|
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))
|
||||||
{
|
{
|
||||||
GetStatusBar()->SetStatusText("Requesting text...");
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnNotebookPageChanged(wxBookCtrlEvent& event)
|
||||||
|
{
|
||||||
|
wxString defaultURL;
|
||||||
|
switch (event.GetSelection())
|
||||||
|
{
|
||||||
|
case Page_Image:
|
||||||
|
defaultURL = "https://www.wxwidgets.org/downloads/logos/blocks.png";
|
||||||
|
break;
|
||||||
|
case Page_Text:
|
||||||
|
defaultURL = "https://api.github.com";
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxTextCtrl* m_getURLTextCtrl;
|
wxNotebook* m_notebook;
|
||||||
wxStaticBoxSizer* m_getImageBox;
|
wxTextCtrl* m_urlTextCtrl;
|
||||||
wxStaticBitmap* m_getStaticBitmap;
|
wxButton* m_startButton;
|
||||||
|
wxStaticBitmap* m_imageStaticBitmap;
|
||||||
|
|
||||||
wxTextCtrl* m_postURLTextCtrl;
|
wxCheckBox* m_postCheckBox;
|
||||||
wxTextCtrl* m_postContentTypeTextCtrl;
|
wxTextCtrl* m_postContentTypeTextCtrl;
|
||||||
wxTextCtrl* m_postRequestTextCtrl;
|
wxTextCtrl* m_postRequestTextCtrl;
|
||||||
wxTextCtrl* m_postResponseTextCtrl;
|
wxTextCtrl* m_textResponseTextCtrl;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WebRequestApp : public wxApp
|
class WebRequestApp : public wxApp
|
||||||
|
Reference in New Issue
Block a user