一个简单的Java程序尝试从Internet下载一些内容:
try (InputStream is = new URL(url).openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
while ((line = br.readLine()) != null) {
result.append(line);
}
}
但是,某些Web服务器返回以下HTTP 403错误吗?
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://mkyong.com
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at java.base/java.net.URL.openStream(URL.java:1139)