Java 代理问题 - 连接超时 [英] Java proxy issues - Connection Timed Out

查看:70
本文介绍了Java 代理问题 - 连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写一个网页抓取工具,但我使用的是代理服务器,这让事情变得非常困难.

I'm writing a web scraper in Java but I'm behind a proxy server and it's making things very difficult.

这是连接代码:

public void scrape(String url, String filename) throws Exception {
    this.url = url;
    this.filename = filename;

    System.out.println("Scraping " + url);
    System.out.println("Saving to \"" + this.filename + "\"");

    try {
        makeConnection();
        createStream();
        writeToFile();
        System.out.println("Scrape was successful");
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}

private void makeConnection() throws Exception {
    // Set proxy info
    System.setProperty("java.net.useSystemProxies", "true");

    URL address = new URL(url);
    connection  = address.openConnection();
}

这是输出:

Scraping http://feeds.bbci.co.uk/news/northern_ireland/rss.xml
Saving to "../rss/northern_ireland.xml"
Error: Connection timed out

有没有更好的方法来设置代理设置?

Is there a better way of setting the proxy settings?

推荐答案

您可以使用 Java 1.5 中引入的 java.net.Proxy 类...http://download.oracle.com/javase/1.5.0/docs/api/java/net/Proxy.html

You can use the java.net.Proxy class introduced in Java 1.5... http://download.oracle.com/javase/1.5.0/docs/api/java/net/Proxy.html

可以在此处找到有关如何使用它的简要说明:http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

A brief writeup of how it is used can be found here: http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

这篇关于Java 代理问题 - 连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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