英文:
How to download the content of the page despite the Error 500?
问题
我想下载这个页面的内容:https://www.hafen-hamburg.de/en/vessels/klein-erna/。在我的代码和开发工具中,我可以看到我收到了500错误:
但我也可以清楚地看到在浏览器中已经加载了页面。如何在出现这个错误的情况下下载页面的内容?
我的代码:
String pageUrl = "https://www.hafen-hamburg.de/en/vessels/klein-erna/";
URL url = new URL(pageUrl);
String content = IOUtils.toString(url, StandardCharsets.UTF_8);
英文:
I want to download the content of the page:
https://www.hafen-hamburg.de/en/vessels/klein-erna/. In my code and DevTools I can see that I'm getting a 500 error:
but I can also clearly see in the browser that the page has been loaded. How can I download the content of the page despite this error?
My code:
String pageUrl = "https://www.hafen-hamburg.de/en/vessels/klein-erna/";
URL url = new URL(pageUrl);
String content = IOUtils.toString(url, StandardCharsets.UTF_8);
答案1
得分: 1
感谢您的评论 (@egeorge),我使用了 HttpClient 库,并获得了我想要的内容。
代码:
String pageUrl = "https://www.hafen-hamburg.de/en/vessels/klein-erna/";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(pageUrl)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
英文:
thanks for your comment (@egeorge) I used the HttpClient library and got exactly what I wanted.
Code:
String pageUrl = "https://www.hafen-hamburg.de/en/vessels/klein-erna/";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(pageUrl)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论