java忽略代理设置 [英] java ignores proxy settings

查看:75
本文介绍了java忽略代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为请求日志设置了一个本地代理服务器,但我的 java 代码忽略了它并直接连接(Windows XP、JDK 1.7).Web 浏览器可以使用它.因此,我编写了用于讨论的测试代码,即使指定了(虚假)代理,它似乎也可以直接连接.使用虚假代理,我预计连接失败,但代码成功,直接连接:

I have set up a local proxy server for request logging but my java code ignores it and connects directly (Windows XP, JDK 1.7). Web browsers work with it. So I wrote test code for discussion that seems to connect directly even if a (bogus) proxy is specified. With the bogus proxy, I would expect connection failure but the code succeeds, connecting directly:

System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "12345");
System.setProperty("http.nonProxyHosts", "noNonProxyHost.com");
URL url = new URL("http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html");
InputStream in = url.openStream();
System.out.println("Connection via bogus proxy succeeded");

代码作为独立的 Java 运行,没有 Maven,没有小程序,没有容器.我有直接的互联网连接.

The code is run as standalone Java, no Maven, no applet, no container. I have a direct internet connection.

推荐答案

在您使用 java.net.URL() 的情况下,如果无法在 http.proxyHost 和 http.proxyPort 处访问代理服务器,则它只会回退并尝试进行直接连接.如果成功,您将不会看到抛出异常,这就是为什么您的代码可以正常工作的原因.但是,当它尝试查找代理时,您应该会看到暂停.
下面的示例代码愉快地获取 URL 并显示它,即使在使用虚假代理设置运行时也没有错误.-Dhttp.proxyHost=bogus -Dhttp.proxyPort=2345 但如果设置正确,将与我的本地代理 localhost 端口 8888 通信

In your case using java.net.URL(), if the proxy server cannot be reached at http.proxyHost and http.proxyPort then it simply falls back and tries to do a direct connect. If that succeeds, you'll see no exception thrown which is why your code works without error. You should see a pause while it tries to find the proxy though.
This sample code below happily fetches the URL and displays it, without error, even when run with bogus proxy settings. -Dhttp.proxyHost=bogus -Dhttp.proxyPort=2345 but will talk to my local proxy localhost port 8888 if set correctly

import java.io.*;
import java.net.URL;
import java.util.*;
public class URLClient {
  private static String sUrl = "http://www.apache.org/";
  public static void main(String[] args) {
    try {
    URL url = new URL(sUrl);
    InputStream is = url.openStream();
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    String output =  s.hasNext() ? s.next() : "";
    System.out.println(output);
    } catch(Throwable e) {
            System.err.println("exception");
    }
  }
}

我最初遇到的问题是 http.proxyHost 和 http.proxyPort 被忽略(谷歌引导我回答你的问题)是这些设置被 apache.commons.httpClient 完全忽略,因为它使用自己的套接字,如here所述.http://cephas.net/blog/2007/11/14/java-commons-http-client-and-http-proxies/

The problem I was originally having with http.proxyHost and http.proxyPort being ignored (Google led me to your question) was that those settings are completely ignored by apache.commons.httpClient because it uses its own sockets, as described here. http://cephas.net/blog/2007/11/14/java-commons-http-client-and-http-proxies/

这篇关于java忽略代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆