prettify and simplify the URL test; use a URL more likely to run a web server than localhost by default
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57665 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -31,7 +31,9 @@
|
|||||||
|
|
||||||
#include "wx/socket.h"
|
#include "wx/socket.h"
|
||||||
#include "wx/url.h"
|
#include "wx/url.h"
|
||||||
#include "wx/wfstream.h"
|
#include "wx/sstream.h"
|
||||||
|
#include "wx/scopeguard.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// resources
|
// resources
|
||||||
@@ -212,7 +214,8 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
|||||||
|
|
||||||
#if wxUSE_URL
|
#if wxUSE_URL
|
||||||
m_menuProtocols = new wxMenu();
|
m_menuProtocols = new wxMenu();
|
||||||
m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL"));
|
m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL\tCtrl-U"),
|
||||||
|
_("Get data from the specified URL"));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Append menus to the menubar
|
// Append menus to the menubar
|
||||||
@@ -235,6 +238,7 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
|||||||
_("Welcome to wxSocket demo: Client\nClient ready\n"),
|
_("Welcome to wxSocket demo: Client\nClient ready\n"),
|
||||||
wxDefaultPosition, wxDefaultSize,
|
wxDefaultPosition, wxDefaultSize,
|
||||||
wxTE_MULTILINE | wxTE_READONLY);
|
wxTE_MULTILINE | wxTE_READONLY);
|
||||||
|
delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text));
|
||||||
|
|
||||||
// Create the socket
|
// Create the socket
|
||||||
m_sock = new wxSocketClient();
|
m_sock = new wxSocketClient();
|
||||||
@@ -577,62 +581,55 @@ void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
|
|||||||
|
|
||||||
void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event))
|
void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event))
|
||||||
{
|
{
|
||||||
// Note that we are creating a new socket here, so this
|
// Ask for the URL
|
||||||
// won't mess with the client/server demo.
|
static wxString s_urlname("http://www.google.com/");
|
||||||
|
wxString urlname = wxGetTextFromUser
|
||||||
|
(
|
||||||
|
_("Enter an URL to get"),
|
||||||
|
_("URL:"),
|
||||||
|
s_urlname
|
||||||
|
);
|
||||||
|
if ( urlname.empty() )
|
||||||
|
return; // cancelled by user
|
||||||
|
|
||||||
// Ask for the URL
|
s_urlname = urlname;
|
||||||
m_text->AppendText(_("\n=== URL test begins ===\n"));
|
|
||||||
wxString urlname = wxGetTextFromUser(_("Enter an URL to get"),
|
|
||||||
_("URL:"),
|
|
||||||
_T("http://localhost"));
|
|
||||||
|
|
||||||
// Parse the URL
|
|
||||||
wxURL url(urlname);
|
|
||||||
if (url.GetError() != wxURL_NOERR)
|
|
||||||
{
|
|
||||||
m_text->AppendText(_("Error: couldn't parse URL\n"));
|
|
||||||
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to get the input stream (connects to the given URL)
|
wxLogMessage("=== URL test begins ===");
|
||||||
m_text->AppendText(_("Trying to establish connection...\n"));
|
wxON_BLOCK_EXIT1( wxLogMessage, "=== URL test ends ===" );
|
||||||
wxYield();
|
|
||||||
wxInputStream *data = url.GetInputStream();
|
|
||||||
if (!data)
|
|
||||||
{
|
|
||||||
m_text->AppendText(_("Error: couldn't read from URL\n"));
|
|
||||||
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print the contents type and file size
|
// Parse the URL
|
||||||
wxString s;
|
wxURL url(urlname);
|
||||||
s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
|
if ( url.GetError() != wxURL_NOERR )
|
||||||
url.GetProtocol().GetContentType().c_str(),
|
{
|
||||||
data->GetSize());
|
wxLogError("Failed to parse URL \"%s\"", urlname);
|
||||||
m_text->AppendText(s);
|
return;
|
||||||
wxYield();
|
}
|
||||||
|
|
||||||
// Get the data
|
// Try to get the input stream (connects to the given URL)
|
||||||
wxFile fileTest(wxT("test.url"), wxFile::write);
|
wxLogMessage("Establishing connection to \"%s\"...", urlname);
|
||||||
wxFileOutputStream sout(fileTest);
|
const std::auto_ptr<wxInputStream> data(url.GetInputStream());
|
||||||
if (!sout.Ok())
|
if ( !data.get() )
|
||||||
{
|
{
|
||||||
m_text->AppendText(_("Error: couldn't open file for output\n"));
|
wxLogError("Failed to retrieve URL \"%s\"", urlname);
|
||||||
m_text->AppendText(_("=== URL test ends ===\n"));
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
data->Read(sout);
|
// Print the contents type and file size
|
||||||
m_text->AppendText(_("Results written to file: test.url\n"));
|
wxLogMessage("Contents type: %s\nFile size: %i\nStarting to download...",
|
||||||
m_text->AppendText(_("Done.\n"));
|
url.GetProtocol().GetContentType(),
|
||||||
m_text->AppendText(_("=== URL test ends ===\n"));
|
data->GetSize());
|
||||||
|
|
||||||
delete data;
|
// Get the data
|
||||||
|
wxStringOutputStream sout;
|
||||||
|
if ( data->Read(sout).GetLastError() != wxSTREAM_EOF )
|
||||||
|
wxLogError("Error reading the input stream.");
|
||||||
|
|
||||||
|
wxLogMessage("Text retrieved from URL \"%s\" follows:\n%s",
|
||||||
|
urlname, sout.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // wxUSE_URL
|
||||||
|
|
||||||
void MyFrame::OnSocketEvent(wxSocketEvent& event)
|
void MyFrame::OnSocketEvent(wxSocketEvent& event)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user