简体   繁体   English

如何使用java测试代理互联网连接?

[英]how to test proxy internet connection using java?

i have some code to test if the proxy server and port is working ,some of the code like this: 我有一些代码来测试代理服务器和端口是否正常工作,一些代码如下:

System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "localhost");
System.getProperties().put("https.proxyPort", "1234");
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "1234");
HttpURLConnection conn = (HttpURLConnection) new URL("https://www.google.com").openConnection();
conn.getContent();
conn.disconnect();

it seems that openConnection() method will do thing like this: 似乎openConnection()方法会做这样的事情:

  1. try to connect given URL using proxy. 尝试使用代理连接给定的URL。
  2. if it fails to use proxy,it will connect URL directly without proxy . 如果它无法使用代理,它将直接连接URL 而无需代理

that's the problem,i meant to test if the proxy is working,but this code won't stop if the proxy can not connect. 这是问题,我的意思是测试代理是否正常工作,但如果代理无法连接,此代码将不会停止。

i also tried to use isReachable() method of InetAddress class,but i get the same result. 我也尝试使用InetAddress类的isReachable()方法,但我得到了相同的结果。

so how could i stop this connection if the proxy doesn't work ,in order to test if the proxy is reachable ? 所以,如果代理不起作用,我怎么能停止这种连接,以测试代理是否可以访问?

System.getProperties().put("proxySet", "true");

That one doesn't do anything. 那个人什么都不做。 It is an urban myth. 这是一个都市神话。 It was part of the defunct 1997 HotJava bean and leaked from there into various books. 它是已经废弃的1997 HotJava bean的一部分,并从那里泄漏成各种书籍。 It has never been part of any JDK. 它从未成为任何JDK的一部分。 Try setting it to false in some situation where you need it on and see for yourself. 尝试在需要的情况下将其设置为false并亲自查看。

Sorry guys, I found out the way to do it. 对不起伙计们,我找到了办法。 I used java.net.Proxy class to open a connection via proxy. 我使用java.net.Proxy类通过代理打开连接。 It's easy to use and works fine. 它易于使用,工作正常。 See Java Networking and Proxies 请参阅Java Networking and Proxies

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM