通过代理连接到Java中的URL

Ran*_*num 4 java

我正在尝试编写一个小的Java程序,使用URL连接库连接到Twitter搜索URL(返回推文的JSON列表)。

我从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();
    }
Run Code Online (Sandbox Code Playgroud)

但由于某些原因,我不断收到以下异常:

in thread "main" java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
Run Code Online (Sandbox Code Playgroud)

我不知道这是由于我编写代码的方式,eclipse设置还是与我的网络有关。我确实配置了用于Internet访问的代理服务器。据我所知,这是正确配置的,因为我正在获取更新,并且可以通过Eclipse安装新软件。我是否需要以某种方式将代理信息放入URL方法中还是其他问题。

Tho*_*gum 5

URL依赖于系统属性进行代理,请尝试按以下方式设置代理:

System.setProperty("http.proxyHost", "yourproxyserver");
System.setProperty("http.proxyPort", "portnumber");
Run Code Online (Sandbox Code Playgroud)