Search code examples
c++downloadwxwidgets

How to download a file in C++\wxWidgets


How may I download a file in C++ with wxWidgets?

Been googling and everything and nothing shows up! Help appreciated!


Solution

  • Use wxHTTP class for that.

    wxHTTP Example Code:

    #include <wx/sstream.h>
    #include <wx/protocol/http.h>
    
    wxHTTP get;
    get.SetHeader(_T("Content-type"), _T("text/html; charset=utf-8"));
    get.SetTimeout(10); // 10 seconds of timeout instead of 10 minutes ...
    
    while (!get.Connect(_T("www.google.com")))
        wxSleep(5);
    
    wxApp::IsMainLoopRunning();
    
    wxInputStream *httpStream = get.GetInputStream(_T("/intl/en/about.html"));
    
    if (get.GetError() == wxPROTO_NOERR)
    {
        wxString res;
        wxStringOutputStream out_stream(&res);
        httpStream->Read(out_stream);
    
        wxMessageBox(res);
    }
    else
    {
        wxMessageBox(_T("Unable to connect!"));
    }
    
    wxDELETE(httpStream);
    get.Close();
    

    If you want more flexible solution consider using libcurl.