wxwidget - WxHTTP - 首先检查互联网连接

发布于 2025-01-12 07:47:46 字数 1713 浏览 1 评论 0原文

参考示例 https://wiki.wxwidgets.org/WxHTTP#Basic_Usage

#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 ...

// this will wait until the user connects to the internet. It is important in case of dialup (or ADSL) connections
while (!get.Connect(_T("www.google.com")))  // only the server, no pages here yet ...
    wxSleep(5);

wxApp::IsMainLoopRunning(); // should return true

// use _T("/") for index.html, index.php, default.asp, etc.
wxInputStream *httpStream = get.GetInputStream(_T("/intl/en/about.html"));

// wxLogVerbose( wxString(_T(" GetInputStream: ")) << get.GetResponse() << _T("-") << ((resStream)? _T("OK ") : _T("FAILURE ")) << get.GetError() );

if (get.GetError() == wxPROTO_NOERR)
{
    wxString res;
    wxStringOutputStream out_stream(&res);
    httpStream->Read(out_stream);

    wxMessageBox(res);
    // wxLogVerbose( wxString(_T(" returned document length: ")) << res.Length() );
}
else
{
    wxMessageBox(_T("Unable to connect!"));
}

wxDELETE(httpStream);
get.Close();

所以现在上面代码中的循环等待直到互联网连接可用。

想要向用户显示一条消息以检查连接并重试(是/否/重试),解决方案应该跨平台工作

已尝试过 wxDialUpManager 和 wxDialUpManager 。 wxURL (参考代码 link1, link2 )但示例代码不起作用。它不会检测互联网是否不可用。

检查互联网连接的最佳方法是什么?

Referring to sample https://wiki.wxwidgets.org/WxHTTP#Basic_Usage

#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 ...

// this will wait until the user connects to the internet. It is important in case of dialup (or ADSL) connections
while (!get.Connect(_T("www.google.com")))  // only the server, no pages here yet ...
    wxSleep(5);

wxApp::IsMainLoopRunning(); // should return true

// use _T("/") for index.html, index.php, default.asp, etc.
wxInputStream *httpStream = get.GetInputStream(_T("/intl/en/about.html"));

// wxLogVerbose( wxString(_T(" GetInputStream: ")) << get.GetResponse() << _T("-") << ((resStream)? _T("OK ") : _T("FAILURE ")) << get.GetError() );

if (get.GetError() == wxPROTO_NOERR)
{
    wxString res;
    wxStringOutputStream out_stream(&res);
    httpStream->Read(out_stream);

    wxMessageBox(res);
    // wxLogVerbose( wxString(_T(" returned document length: ")) << res.Length() );
}
else
{
    wxMessageBox(_T("Unable to connect!"));
}

wxDELETE(httpStream);
get.Close();

So now the while loop in above code waits till internet connection is available.

Wanted to display a message to user to check connection and try again (Yes/No/Retry), solution should work crossplatform

Have tried wxDialUpManager & wxURL ( reference code link1, link2 ) but sample code doesn't work. It doesn't detect if internet is not available.

What is best way to check for internet connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2025-01-19 07:47:46

将 GUI 和检查线程中连接的代码分开听起来更好。

这些线程独立运行,并将连接状态传达给主 gui 线程,而不会干扰 gui 事件循环。

看看这个作为参考
https://doc.qt.io/qt-5/thread-basics。 html

It sounds better to separate the gui and code that checks connection in a thread .

The threads run standalone and communicates connection state to the main gui thread without perturbing gui event loop.

look at this for reference
https://doc.qt.io/qt-5/thread-basics.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文