Android 在 HttpClient 中收到 403 后得到响应

发布于 2024-11-28 23:17:01 字数 1025 浏览 1 评论 0原文

我有这样的代码:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(server);

try {
    JSONObject params = new JSONObject();
    params.put("email", email);

    StringEntity entity = new StringEntity(params.toString(), "UTF-8");
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
    httpPost.setEntity(entity);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpClient.execute(httpPost, responseHandler);
    JSONObject response = new JSONObject(responseBody);
        fetchUserData(response);

    saveUserInfo();

    return true;
} catch (ClientProtocolException e) {
    Log.d("Client protocol exception", e.toString());
    return false;
} catch (IOException e) {
    Log.d`enter code here`("IOEXception", e.toString());
    return false;
} catch (JSONException e) {
    Log.d("JSON exception", e.toString());
    return false;
}

即使我有 HTTP 403 Forbidden 来获取错误消息,我也希望得到响应

I have a code like this:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(server);

try {
    JSONObject params = new JSONObject();
    params.put("email", email);

    StringEntity entity = new StringEntity(params.toString(), "UTF-8");
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
    httpPost.setEntity(entity);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpClient.execute(httpPost, responseHandler);
    JSONObject response = new JSONObject(responseBody);
        fetchUserData(response);

    saveUserInfo();

    return true;
} catch (ClientProtocolException e) {
    Log.d("Client protocol exception", e.toString());
    return false;
} catch (IOException e) {
    Log.d`enter code here`("IOEXception", e.toString());
    return false;
} catch (JSONException e) {
    Log.d("JSON exception", e.toString());
    return false;
}

And i want to have a response even if I have HTTP 403 Forbidden to get error message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-12-05 23:17:01

BasicResponseHandler 仅在返回成功代码 (2xx) 时才返回您的数据。但是,您可以非常轻松地编写自己的 ResponseHandler 来始终以 String 形式返回响应正文,例如,

    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    @Override
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        return EntityUtils.toString(response.getEntity());
    }
    };

或者,您可以使用 其他重载执行方法 上HttpClient 不需要 ResponseHandler 并直接返回 HttpResponse。然后以相同的方式调用 EntityUtils.toString(response.getEntity()) 。

要获取响应的状态代码,您可以使用 HttpResponse.getStatusLine().getStatusCode() 并与 HttpStatus 类中的静态整数之一进行比较。例如,代码“403”是HttpStatus.SC_FORBIDDEN。您可以根据返回的状态代码采取与您的应用程序相关的特定操作。

The BasicResponseHandler only returns your data if a success code (2xx) was returned. However, you can very easily write your own ResponseHandler to always return the body of the response as a String, e.g.

    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    @Override
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        return EntityUtils.toString(response.getEntity());
    }
    };

Alternatively, you can use the other overloaded execute method on HttpClient which does not require a ResponseHandler and returns you the HttpResponse directly. Then call EntityUtils.toString(response.getEntity()) in the same way.

To get the status code of a response, you can use HttpResponse.getStatusLine().getStatusCode() and compare to to one of the static ints in the HttpStatus class. E.g. code '403' is HttpStatus.SC_FORBIDDEN. You can take particular actions as relevant to your application depending on the status code returned.

内心旳酸楚 2024-12-05 23:17:01

根据 基本响应处理程序

如果响应不成功(>= 300 状态代码),则抛出 HttpResponseException

您可以捕获这种类型的异常(注意:您已经捕获了此异常的超类型 ClientProtocolException),并且您可以在该 catch 块中放置一些自定义逻辑,以便在遇到错误时创建/保存一些响应情况,例如403。

According to the documentation for BasicResponseHandler:

If the response was unsuccessful (>= 300 status code), throws an HttpResponseException.

You could catch this type of exception (Note: you are already catching the supertype of this exception ClientProtocolException) and you could put some custom logic in that catch block to create / save some response when you encounter an error situation, such as the 403.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文