![](/img/trans.png)
[英]Getting “java.io.IOException: Server returned HTTP response code: 403 for URL: ” in java code
[英]Error : java.io.IOException: Server returned HTTP response code: 403 for URL
我是開發新手,我正在使用 IntelliJ IDEA 2022.1(社區版)。 例如我想連接和檢索網頁: www.carrefour.fr我有以下錯誤:
java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.carrefour.fr/
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:224)
at Main.main(Main.java:111)
Process finished with exit code 0
我在互聯網上搜索了很多並進行了測試,但它並沒有解決問題。 如何糾正問題?
import java.io.IOException;
import java.net.URL;
import java.net.*;
import java.io.*;
import java.nio.charset.Charset;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
StringBuilder content=new StringBuilder();
// Use try and catch to avoid the exceptions
try
{
URL url=new URL("https://www.carrefour.fr"); // creating a url object
// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URLConnection urlConnection=url.openConnection(); // creating a urlconnection object
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0 Unique/100.7.9656.57");
// wrapping the urlconnection in a bufferedreader
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), Charset.forName("UTF-8")));
String line;
// reading from the urlconnection using the bufferedreader
while((line=bufferedReader.readLine())!=null)
{
content.append(line+"\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(content.toString());
}
}
您的代碼沒有問題,但該網站不希望人們在其上運行爬蟲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.