如何在出现500错误的情况下下载页面的内容?

huangapple go评论69阅读模式
英文:

How to download the content of the page despite the Error 500?

问题

我想下载这个页面的内容:https://www.hafen-hamburg.de/en/vessels/klein-erna/。在我的代码和开发工具中,我可以看到我收到了500错误:

如何在出现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:

如何在出现500错误的情况下下载页面的内容?

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 = &quot;https://www.hafen-hamburg.de/en/vessels/klein-erna/&quot;;
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder().uri(URI.create(pageUrl)).build();
    HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response.body());

huangapple
  • 本文由 发表于 2023年2月24日 04:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定