使用 java 中的代理代码连接到站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3035973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to a site using proxy code in java
提问by Nithin
I want to connect to as site through proxy in java. This is the code which I have written:
我想通过java中的代理连接到站点。这是我写的代码:
public class ConnectThroughProxy
{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080));
public static void main(String[] args)
{
try
{
URL url = new URL("http://www.rgagnon.com/javadetails/java-0085.html");
URLConnection connection=url.openConnection();
String encoded = new String(Base64.encode(new String("user_name:pass_word").getBytes()));
connection.setDoOutput(true);
connection.setRequestProperty("Proxy-Authorization","Basic "+encoded);
String page="";
String line;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line=in.readLine()) != null)
{
page.concat(line + "\n");
}
System.out.println(page);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
While trying to run this code it throws the following error:
在尝试运行此代码时,它会引发以下错误:
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic dXNlcl9uYW1lOnBhc3Nfd29yZA==
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)
at test.ConnectThroughProxy.main(ConnectThroughProxy.java:30)
java.lang.IllegalArgumentException:消息头值中的非法字符:基本 dXNlcl9uYW1lOnBhc3Nfd29yZA==
在 sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)
在 sun.net.www.protocol。 http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)
at test.ConnectThroughProxy.main(ConnectThroughProxy.java:30)
Any Idea how to do it?
任何想法如何做到这一点?
回答by Curtis
If you're just trying to make HTTP requests through an HTTP proxy server, you shouldn't need to go to this much effort. There's a writeup here: http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
如果您只是尝试通过 HTTP 代理服务器发出 HTTP 请求,则无需花费太多精力。这里有一篇文章:http: //java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
But it basically boils down to just setting the http.proxyHost and http.proxyPort environment properties, either on the command line, or in code:
但它基本上归结为只是在命令行或代码中设置 http.proxyHost 和 http.proxyPort 环境属性:
// Set the http proxy to webcache.mydomain.com:8080 System.setProperty("http.proxyHost", "webcache.mydomain.com"); System.setProperty("http.proxyPort", "8080"); // Next connection will be through proxy. URL url = new URL("http://java.sun.com/"); InputStream in = url.openStream(); // Now, let's 'unset' the proxy. System.clearProperty("http.proxyHost"); // From now on HTTP connections will be done directly.
回答by pezetko
It seems to me, that you are not using your Proxy instance at all. I think you should pass it when you are creating URLConnectioninstance:
在我看来,您根本没有使用 Proxy 实例。我认为您应该在创建URLConnection实例时传递它:
URLConnection connection=url.openConnection(proxy);
Setting of environment properties http.proxyis easier and when using some 3rd party libraries without Proxy instance passing support only possible solution, but its drawback is that it is set globally for the whole process.
环境属性http.proxy 的设置更容易,当使用一些没有代理实例传递的 3rd 方库时,只支持可能的解决方案,但它的缺点是它在整个过程中是全局设置的。
回答by Marcello de Sales
I was using the Google Data APIs and the only way I got the proxy settings to work was to provide ALL the parameters related to proxy, even thought they are set to be empty:
我正在使用谷歌数据 API,让代理设置工作的唯一方法是提供与代理相关的所有参数,即使它们被设置为空:
/usr/java/jdk1.7.0_04/bin/java -Dhttp.proxyHost=10.128.128.13
-Dhttp.proxyPassword -Dhttp.proxyPort=80 -Dhttp.proxyUserName
-Dhttps.proxyHost=10.128.128.13 -Dhttps.proxyPassword -Dhttps.proxyPort=80
-Dhttps.proxyUserName com.stackoverflow.Runner
Where, username and password are NOT required, and the same http and https servers are set to be the same, as well as the port number (if that's your case as well). Note that the same HTTP proxy is also provided as the HTTPS server, as well as its port number (reference from https://code.google.com/p/syncnotes2google/issues/detail?id=2#c16).
其中,不需要用户名和密码,并且相同的 http 和 https 服务器设置为相同,以及端口号(如果您也是这种情况)。请注意,还提供了相同的 HTTP 代理作为 HTTPS 服务器及其端口号(参考来自https://code.google.com/p/syncnotes2google/issues/detail?id=2#c16)。
If your Java class has an instance of the class "URL", it should pick those configurations up...
如果你的 Java 类有一个“URL”类的实例,它应该选择那些配置......