apache 我怎样才能得到 HttpResponseException 背后的实际错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1490341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:12:25  来源:igfitidea点击:

How can I get the actual error behind HttpResponseException?

javaapachehttppost

提问by Dan Dascalescu

I'm using Apache HttpComponents Client to POST to a server that returns JSON. The problem is that if the server returns a 400 error, I seem to have no way of telling what the error was from Java (had to resort to a packet sniffer so far - ridiculous). Here is the code:

我正在使用 Apache HttpComponents 客户端 POST 到返回 JSON 的服务器。问题是,如果服务器返回 400 错误,我似乎无法判断错误来自 Java(到目前为止不得不求助于数据包嗅探器 - 荒谬)。这是代码:

HttpClient httpclient = new DefaultHttpClient();
params.add(new BasicNameValuePair("format", "json"));
params.add(new BasicNameValuePair("foo", bar));

HttpPost httppost = new HttpPost(uri);
// this is how you set the body of the POST request
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

String responseBody = "";
try {
    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    responseBody = httpclient.execute(httppost, responseHandler);
} catch(HttpResponseException e) {
    String error = "unknown error";
    if (e.getStatusCode() == 400) {
        // TODO responseBody and e.detailMessage are null here, 
        // even though packet sniffing may reveal a response like
        // Transfer-Encoding: chunked
        // Content-Type: application/json
        //
        // 42
        // {"error": "You do not have permissions for this operation."}
        error = new JSONObject(responseBody).getString("error");  // won't work
        }
    // e.getMessage() is ""
}

What am I doing wrong? There must be an easy way to get the message of a 400 error. This is elementary.

我究竟做错了什么?必须有一种简单的方法来获取 400 错误消息。这是基本的。

回答by ZZ Coder

Why do you use BasicResponseHandler()? The handler is doing that for you. That handler is just an example and shouldn't be used in real code.

为什么要使用 BasicResponseHandler()?处理程序正在为您执行此操作。该处理程序只是一个示例,不应在实际代码中使用。

You should either write your own handler or call execute without a handler.

您应该编写自己的处理程序或在没有处理程序的情况下调用 execute。

For example,

例如,

        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        responseBody = entity.getContent();

        if (statusCode != 200) {
            // responseBody will have the error response
        }

回答by pstanton

responseBody will always be null if an exception is thrown while assigning a value to it.

如果在为其分配值时抛出异常,则 responseBody 将始终为 null。

besides that it's specific behaviour of the implementation - ie Apache HttpClient.

除此之外,它是实现的特定行为 - 即 Apache HttpClient。

It looks like it doesn't maintain any of the detailed information in the exception (obviously).

看起来它没有维护异常中的任何详细信息(显然)。

I'd load up the source code for HttpClient and debug that.

我会加载 HttpClient 的源代码并调试它。

but first check if there's anything in the e.getCause()...

但首先检查 e.getCause() 中是否有任何内容...

hope that helps.

希望有帮助。