1. ping may be blocked by the gateway. For example, here we have 2 and 80 ports. Some networks that require authentication, such as ChinaUnicom’s wifi, will block all 80 ports before logging in. DNATThen it redirects to the login page, leading to misjudgment. 3. 443 port. Generally certified networks will not DNAT this port. Even if it is done, it will cause a certificate error. There are not many domestic HTTPS websites, and Baidu will also have a certificate under normal circumstances. mistake.
So it is recommended that wget/curl have a HTTP page with fixed content, such as http://example.com/ (this domain name really exists), and then judge the content. If different is detected, the user can also be prompted to "Your network" You may need to log in to use". And even if the judgment is wrong, the user experience will be the same.
How to detect whether he is connected to the external network? This problem itself is difficult to solve because you cannot define "connected to the external network". So there are only targeted approaches:
This is what Nintendo 3DS does: get the domain name conntest.nintendowifi.net. If it can be opened and returns to This is test.html page, then 3DS will think that it is connected to the Internet. The entire process includes domain name resolution and http 80 requests.
As for different environments, it is different. For example, if one day conntest.nintendowifi.net is blocked or banned, this method will not take effect. Some networks prohibit ping, and the ping method does not work.
Just use a socket to connect to port 80 of a large website.
1.
ping
may be blocked by the gateway. For example, here we have2 and
80
ports. Some networks that require authentication, such asChinaUnicom
’s wifi, will block all 80 ports before logging in.DNAT
Then it redirects to the login page, leading to misjudgment.3.
443
port. Generally certified networks will notDNAT
this port. Even if it is done, it will cause a certificate error. There are not many domesticHTTPS
websites, and Baidu will also have a certificate under normal circumstances. mistake.So it is recommended that
wget/curl
have aHTTP
page with fixed content, such ashttp://example.com/
(this domain name really exists), and then judge the content. If different is detected, the user can also be prompted to "Your network" You may need to log in to use". And even if the judgment is wrong, the user experience will be the same.ping www.baidu.com
How to detect whether he is connected to the external network? This problem itself is difficult to solve because you cannot define "connected to the external network".
So there are only targeted approaches:
This is what Nintendo 3DS does:
get the domain name conntest.nintendowifi.net. If it can be opened and returns to This is test.html page, then 3DS will think that it is connected to the Internet.
The entire process includes domain name resolution and http 80 requests.
As for different environments, it is different. For example, if one day conntest.nintendowifi.net is blocked or banned, this method will not take effect.
Some networks prohibit ping, and the ping method does not work.
Directly execute the command ping or other?
ping command
For example, ping www.sina.com to check the packet loss rate.
wget files, check size
Famous
curl ifconfig.me
Just like surfing the Internet through a browser, we get the Baidu homepage page to see if there is the word html and the word Baidu:
will get the html of an index. If there is success, if there is no success, there will be no network. Haha