简体   繁体   English

强制 java.net.HttpUrlConnection 返回 GET 响应,而不考虑 Http-Status-Code

[英]Force java.net.HttpUrlConnection to return GET-response regardless of Http-Status-Code

I'm working on a HTTP-Client to sent GET-Requests to an API, which responds with proper JSON-Objects even when the HTTP-Status Codes contains an Error such as 401.我正在使用 HTTP 客户端将 GET 请求发送到 API,即使 HTTP 状态代码包含诸如 401 之类的错误,它也会以正确的 JSON 对象响应。

public String get(String url){
        URL target;
        HttpURLConnection connection;
        int code = 200;
        BufferedReader reader;
        String inputLine;
        String result = null; 

        try {
            target = new URL(url);
        } catch (MalformedURLException ex) {
            return result;
        }

        try {
            connection = (HttpURLConnection)target.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            //code = connection.getResponseCode();
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            result = "";
            while ((inputLine = reader.readLine()) != null){ 
                result += inputLine;
            }
            reader.close();
        } catch (IOException ex) {
            return "...";
        }

        return result;
}

When that's the case, the IOException is thrown and the response isn't written.在这种情况下,会抛出 IOException 并且不会写入响应。 However, I want to receive the response regardless of the HTTP-Status-Code and hande error handling myself.但是,我想接收响应,而不管 HTTP-Status-Code 和hande 错误处理自己。 How can I achieve this?我怎样才能做到这一点?

I don't believe you can do that , but there's https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#getErrorStream-- for getting the payload in case of an error.我不相信你能做到这一点,但是有https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#getErrorStream--用于在发生错误时获取有效负载.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果服务器返回错误状态,java.net.HttpUrlConnection 是否总是抛出 IOException? - Does java.net.HttpUrlConnection always throw IOException if there is an error status return by Server? 使用java.net.HttpURLConnection时是否可以转储HTTP标头? - Is it possible to dump the HTTP Headers when using java.net.HttpURLConnection? 如何获取java.net.HttpURLConnection使用的源端口? - How to get the source port being used by java.net.HttpURLConnection? 处理 GET 请求 - Apache HttpClient 与 java.net.HttpURLConnection - Handling GET requests - Apache HttpClient vs java.net.HttpURLConnection 为什么java.net.HttpURLConnection成为sun.net.www.protocol.http.HttpURLConnection? - Why the java.net.HttpURLConnection become sun.net.www.protocol.http.HttpURLConnection? java.net.httpurlconnection 不起作用 - java.net.httpurlconnection just wont work 应该不鼓励使用java.net.HttpURLConnection,因为org.apache.http.client.HttpClient更好吗? - Should use of java.net.HttpURLConnection be discouraged as org.apache.http.client.HttpClient is way better ? 多线程Java应用程序中java.net.HttpURLConnection的不同实例 - Different instance of java.net.HttpURLConnection in multithreaded java application 为什么java.net.HttpURLConnection响应不明字符? - why java.net.HttpURLConnection responsed unidentified characters? 处理与服务器不存在的java.net.HttpURLConnection - Handling java.net.HttpURLConnection to server that does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM