2012-08-27 59 views

回答

6

使用wxHTTP類。

wxHTTP示例代碼:

#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(); 

如果你想要更多的靈活的解決方案考慮使用libcurl

0

您沒有定義「下載文件」對您意味着什麼。

如果要使用HTTP檢索某些內容,則應使用HTTP客戶端庫,如libcurl,併發出相應的HTTP GET請求。

1

取決於你想從哪裏下載它,以及文件服務器如何允許下載文件。服務器可能使用FTP或HTTP,或者更隱晦的東西。沒有辦法從你的問題中知道沒有任何有用的信息。

一般來說,我不會在這個任務中使用wxWidgets。 wxWidgets是一個圖形用戶界面(GUI frmaework),它提供了一些額外的功能,可以用於或不用於你的情況。

1

HTTP安德烈斯建議,從FTP使用wxFTP

wxFTP ftp; 

// if you don't use these lines anonymous login will be used 
ftp.SetUser("user"); 
ftp.SetPassword("password"); 

if (!ftp.Connect("ftp.wxwindows.org")) 
{ 
    wxLogError("Couldn't connect"); 
    return; 
} 

ftp.ChDir("/pub"); 
wxInputStream *in = ftp.GetInputStream("wxWidgets-4.2.0.tar.gz"); 
if (!in) 
{ 
    wxLogError("Coudln't get file"); 
} 
else 
{ 
    size_t size = in->GetSize(); 
    char *data = new char[size]; 
    if (!in->Read(data, size)) 
    { 
     wxLogError("Read error"); 
    } 
    else 
    { 
     // file data is in the buffer 
     ... 
    } 

    delete [] data; 
    delete in; 
} 

http://docs.wxwidgets.org/stable/wx_wxftp.html#wxftp