wxwidget - WxHTTP - 首先检查互联网连接
参考示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 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