我刚刚开始使用
Java网络协议.我试图使用我的代理服务器连接到互联网.当我看到’
https://www.tutorialspoint.com/javaexamples/net_poxy.htm‘的帖子时,他们将http.proxyHost属性设置为’proxy.mycompany1.local’.我知道我可以将其设置为我的代理服务器IP,但我很好奇地知道为什么我的程序仍然可以工作,即使我将其设置为一些随机字符串,如“abcd”.
答:“proxy.mycompany1.local”代表什么?
B.我的程序如何工作,即使我将http.proxyHost设置为“abcd”?
以下是我的工作计划:
import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.URI; import java.net.URL; public class TestProxy { public static void main(String s[]) throws Exception { try { System.setProperty("http.proxyHost","abcd"); System.setProperty("http.proxyPort","8080"); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); } catch (Exception e) { e.printStackTrace(); System.out.println(false); } Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI("http://www.google.com")).iterator().next(); System.out.println("proxy Type : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("No Proxy"); } else { System.out.println("proxy hostname : " + addr.getHostName()); System.out.println("proxy port : " + addr.getPort()); } } }
这是输出:
200 : OK proxy Type : HTTP proxy hostname : abcd proxy port : 8080
解决方法
首先,根据
System Properties tutorial.
Warning: Changing system properties is potentially dangerous and
should be done with discretion. Many system properties are not reread
after start-up and are there for informational purposes. Changing some
properties may have unexpected side-effects.
而且我的经验表明,当您更改* .proxyHost属性时,您可能会在系统上收到令人不快的问题.所以我不建议你改变这个任务的系统属性.
更好地使用像:
//Proxy instance,proxy ip = 127.0.0.1 with port 8080 Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",8080)); conn = new URL(urlString).openConnection(proxy);
和代理授权:
Authenticator authenticator = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user","mypassword".tocharArray()); } }; Authenticator.setDefault(authenticator);
现在回到主要问题:
A.“proxy.mycompany1.local”只是例子,你可以使用任何主机名
B.类URL通过Socket使用java.net.PlainSocketImpl类.它尝试解析代理主机名’abcd’,吞下错误并直接转到谷歌.只是试着玩这个代码:
import java.net.*; import java.io.*; public class RequestURI { public static void main(String[] args) { int port = 8181; long startTime = System.currentTimeMillis(); try { // System.getProperties().setProperty("http.proxyHost","abcd"); // System.getProperties().setProperty("http.proxyPort",Integer.toString(port)); URL url = new URL("http://google.com"); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); int resp = uc.getResponseCode(); if (resp != 200) { throw new RuntimeException("Failed: Fragment is being passed as part of the RequestURI"); } } catch (IOException e) { e.printStackTrace(); } System.out.println("Run time in ms ="+ (System.currentTimeMillis() - startTime)); } }