gpt4 book ai didi

java - 如何修复 java.io.IOException : Server returned HTTP response code: 403

转载 作者:行者123 更新时间:2023-12-02 10:55:49 26 4
gpt4 key购买 nike

目前我正在使用此代码读取 xml 文件,它在我的个人 Ubuntu PC 上运行良好

   URL url = new URL("https://www.google.com/site-map-all.xml");
InputStream inputFile = url.openStream();
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();

但是当我在 Ubuntu 服务器中运行相同的代码时显示错误

java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.google.com/sitemap.xml at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)

谁能帮我找出问题所在吗?服务器问题出在哪里?

最佳答案

我只需要设置用户代理

    URL url = new URL("https://www.google.com/sitemap.xml");
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "
+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
InputStream inputFile = urlc.getInputStream();

关于java - 如何修复 java.io.IOException : Server returned HTTP response code: 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51763610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com