java 获取400的错误信息_java – 如何获得HttpResponseException背后的实际错误?

我正在使用Apache HttpComponents Client来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 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 ""

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值