可公开访问的URL抛出IOException

时间:2015-10-09 10:10:16

标签: java io inputstream urlconnection

我想访问链接http://www.nation.co.ke/business/seedsofgold/Egg-imports-from-Uganda-hatch-big-losses-for-farmers/-/2301238/2897930/-/dpeqesz/-/index.html

该链接可公开访问,甚至可以使用curl

加载

但是在Java代码中,它会抛出Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.nation.co.ke/business/seedsofgold/Egg-imports-from-Uganda-hatch-big-losses-for-farmers/-/2301238/2897930/-/dpeqesz/-/index.html

这是代码:

/**
 * 
 * @param url the HTML page
 * @throws IOException
 */
public static String getPage(String url) throws IOException {
    URL u = new URL(url);
    URLConnection conn = u.openConnection();

    String mime = conn.getContentType();
    if( !StringUtils.containsIgnoreCase(mime, "text/html") ) {
        return null; // don't continue if not HTML
    }
    else {

        // read the response body, using BufferedReader for performance
        InputStream in = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
        int n = 0, totalRead = 0;
        char[] buf = new char[1024];
        StringBuilder content = new StringBuilder();

        // read until EOF or first 16384 characters
        while (totalRead < 16384 && (n = reader.read(buf, 0, buf.length)) != -1) {
            content.append(buf, 0, n);
            totalRead += n;
        }
        reader.close();

}

错误发生在:

       InputStream in = conn.getInputStream();

相同的代码可以与其他网址一起使用。

2 个答案:

答案 0 :(得分:1)

尝试添加

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

URLConnection conn = u.openConnection();之后立即进行连接。当没有设置正确的代理时,许多网站会阻止网站访问。

答案 1 :(得分:1)

如果您收到HTTP 403状态代码,则表示出于某种原因禁止访问由URL标识的资源。

Web服务器可以返回403 Forbidden HTTP状态代码,以响应客户端对网页或资源的请求,以指示可以访问服务器并理解该请求,但拒绝采取任何进一步的操作。 / p>

您可以参考HTTP 403 status code