gpt4 book ai didi

java 。远程服务器返回 503 错误页面,其中包含一些文本。如何获取这段文字?

转载 作者:行者123 更新时间:2023-12-01 15:19:44 25 4
gpt4 key购买 nike

以下服务器错误:

If an error occurred, a HTTP status code 503 (Service Unavailable) will be returned. The MIME type will be set in accordance with the format parameter that was specified in the request. The body of the the returned page will contain the string "Error:" followed by a string with further details. If XHTML was requested, this message will appear somewhere in the document inside a

element. If plain text was requested, it will appear on the first line in the document.

因此,我发送带有错误参数的请求 -> 服务器返回错误 503 和包含描述问题所在文本的页面。

我使用下面的java代码:

static BufferedReader getReader(String urlString) throws IOException {

HttpURLConnection connection;
BufferedReader reader = null;
URL url = new URL(urlString);

connection = (HttpURLConnection) url.openConnection();

reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

System.out.println(urlString);

return reader;
}

当服务器返回 503 时 - java 抛出异常。但是,我怎样才能获得服务器返回的文本?

最佳答案

使用 HttpURLConnection 类的 getResponseCode()getResponseMessage() 方法。

在构建连接对象之后但在从连接获取流之前调用它们。

关于 java 。远程服务器返回 503 错误页面,其中包含一些文本。如何获取这段文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11118806/

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