简体   繁体   English

通过代理连接到Java中的URL

[英]connecting to a URL in Java through a proxy

I'm trying to write a small java program that connects to a twitter search URL (which returns a JSON list of tweets) using the URL connection libary. 我正在尝试使用URL连接库编写一个小型java程序,该程序连接到Twitter搜索URL(返回推文的JSON列表)。

My code which is taken from the java tutorials looks like : 我的代码取自java教程,如下所示:

        public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://search.twitter.com/search.json?q=hi");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }

but for some reason I keep getting the following Exception: 但由于某种原因,我不断得到以下例外:

in thread "main" java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)

I don't know if this is something due to the way I've written the code, and eclipse setting or something to do with my network. 我不知道这是否是由于我编写代码的方式,eclipse设置或与我的网络有关。 I do have a proxy server configured for internet access. 我确实有一个配置为Internet访问的代理服务器。 As far as I know this is properly configured because I am getting updates and can install new software through eclipse. 据我所知,这是正确配置的,因为我正在获取更新,可以通过eclipse安装新软件。 Do I need to put the proxy information in the URL method somehow or is something else the problem. 我是否需要以某种方式将代理信息放在URL方法中,或者是其他问题。

URL rely on System properties for proxy, try setting proxy like this: URL依赖于代理的系统属性,尝试设置代理如下:

System.setProperty("http.proxyHost", "yourproxyserver");
System.setProperty("http.proxyPort", "portnumber");

Unfortunately, a correct proxy setup in Eclipse seems not to help with proxying Java programs started in Eclipse. 遗憾的是,Eclipse中正确的代理设置似乎无助于代理在Eclipse中启动的Java程序。 Similarly, setting the Java system settings to use the systemwide proxy settings doesn't either. 同样,将Java系统设置设置为使用系统范围的代理设置也不会。 Not when you have a proxy that requires authentication, anyway. 无论如何,当您拥有需要身份验证的代理时。

As Thomas Johan Eggum said, if you have a "normal," non-authenticating proxy then setting the two JVM variables http.proxyHost and http.proxyPort either in the command line via -D or programmatically (see below) will take care of things. 正如Thomas Johan Eggum所说,如果你有一个“正常的”非认证代理,那么在命令行中通过-D或以编程方式(见下文)设置两个JVM变量http.proxyHosthttp.proxyPort将处理事情。

For an authenticating proxy server, ie one that wants to see a user ID and password, many people recommend setting http.proxyUser and http.proxyPassword . 对于身份验证代理服务器,即想要查看用户ID和密码的服务器,很多人建议设置http.proxyUserhttp.proxyPassword That's bad advice, because these don't work. 这是不好的建议,因为这些不起作用。 Apparently they are not defined in the Java docs. 显然它们没有在Java文档中定义。

Unfortunately, it looks like the way to "do" authentication is to use an Authenticator, programmatically. 不幸的是,看起来“做”身份验证的方式是以编程方式使用Authenticator。 If you're going to do that, you might as well do the whole thing programmatically, ie including host and port. 如果你打算这样做,你也可以通过编程方式完成整个过程,即包括主机和端口。 Here's how I got that to work: 这是我如何工作:

   public static void main(String[] args) {
      try {

         System.setProperty("http.proxyHost", "my.proxy.host");
         System.setProperty("http.proxyPort", "8080-or-whatever-proxy-port");
         Authenticator.setDefault(new DummyAuthenticator());

         /* do your main program stuff */             

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private static class DummyAuthenticator extends Authenticator {
      public PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(
               "my-user-id", "my-password".toCharArray()
               );
      }
   }

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

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