dooid3005 2018-04-27 06:00
浏览 52
已采纳

从应用程序检查Internet连接

I need to check if the user is connected to internet before I can proceed.

I am hitting the endpoint using HttpClient as follows:

client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.SetBasicAuth(username, password)
res, err := client.Do(req)

if err != nil {
    fmt.Println(err)
    ui.Failed("Check your internet connection")
}

1) I need to show clear messages to the user if the user is not connected to the internet in this case, display "Please check your internet connection"

2) In case the server is not responding, and receives 504 bad gateway, display "504 Bad gateway"

Can someone help how to proceed and distinguish between these two scenarios and I would like to display only simple messages and not the entire error messages received from the server.

  • 写回答

2条回答 默认 最新

  • dongwuxie7976 2018-04-27 08:22
    关注

    Checking for an established Internet connection isn't as simple as making a single HTTP request to an arbitray URL, like Ivan de la Beldad suggests. This can fail for any number of reasons, none of which will necessarily stop you from doing what you actually intend to do with the connection. To name a few:

    • clients3.google.com may be deliberatly blocked by the local network (firewall, corporate or school proxy) or any en-route network (think Great Firewall of China)
    • clients3.google.com may be unreachable for some clients due to network outages
    • clients3.google.com itself may block the client for whatever reason (perhaps unintentionally)
    • clients3.google.com may have an outage
    • port 80 and 443 may work fine, but all other ports are blocked by shitty hotel/coffee shop wifi
    • shitty hotel/coffee shop wifi presents fake TLS certificates to clients, so HTTPS requests will fail in many cases

    So instead of relying on a single arbitrary HTTP request it is much better to send some kind of liveliness probe to whatever service(s) you actually want to use.

    If your app wants to communicate with an API, see if there is a health or status endpoint that you can call. If there isn't, look for some kind of cheap no-op. And try not to tell users to simply "check their Internet connection". Try to at least explain why your app concludes that there might be an issue "We can't connect to Twitter right now. If you are connected to the Internet try again in a few minutes" is much better.

    On the off-chance that you really only want to check if the Internet itself is available, I would suggest making a DNS query to several DNS servers on the Internet. DNS is not likely to be blocked through local policies and cheaper than HTTP requests. Pick your DNS queries wisely and be prepared for NXDOMAIN responses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料