Je voudrais accéder au lien http://www.nation.co.ke/business/seedsofgold/Egg-imports-from-Uganda-hatch-big-losses-for-farmers/-/23/2897930/-/dpeqesz/-/index.html
URL accessible au public de jeter IOException
Le lien est accessible au public, et peut même charger en utilisant curl
Mais dans le code Java, il jette 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
C'est le Code:
/**
*
* @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();
}
L'erreur est lancée sur:
InputStream in = conn.getInputStream();
Le même code fonctionne très bien avec d'autres URL.
Ceci fait. Merci. –