我想訪問該鏈接http://www.nation.co.ke/business/seedsofgold/Egg-imports-from-Uganda-hatch-big-losses-for-farmers/-/23/2897930/-/dpeqesz/-/index.html
可公開訪問的URL拋出IOException異常
的聯繫是公開訪問,甚至可以加載使用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/-/23/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();
相同的代碼可以很好地與其他URL一起使用。
這樣做了。謝謝。 –