如果401 http响应,则无法获得Responsebody

时间:2014-06-17 21:26:00

标签: http rest httpclient

我编写了代码来对服务器进行休息API http调用。 如果状态200 ok,我可以提取responseBody。 但是如果是401,我将responseBody视为null。当我使用firefox插件httpRequester发出相同的请求时,我可以看到一个响应体,当我调用方法调用getResponseBodyasString()时它不会返回。 我把客户放在下面。 请帮忙

` HttpMethod request = new PostMethod(url);             HttpMethodParams params = new HttpMethodParams();

        log.info("getResponseAsString: queryString = " + queryString);

        request.setQueryString(queryString);
        request.addRequestHeader(HttpConstants.HEADER_ACCEPT, "application/json");

        String response = null;

        try {

            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            final HttpClient httpClient = new HttpClient(connectionManager);
            httpClient.executeMethod(request);

            final int status = request.getStatusCode();
            log.info("getResponseAsString: Execute Method called: status = " + status);

            if (status == 200) {
                response = request.getResponseBodyAsString();
            }else if(status == 401 ){
                log.info("UnAuthorized user....");
                response = request.getResponseBodyAsString();
            }else{

                log.info("Generic Error occured....");
                response = request.getResponseBodyAsString();
            }

            request.releaseConnection();
            log.info("getResponseAsString: Release Connection Called");

        } catch (IOException e) {
            log.error(e.toString());
            e.printStackTrace();
        }

        return response;

`

响应对象为null状态!= 200.但返回的响应低于(包含firefox插件httpRequester)

{"exception":{"message":"Invalid Password!!","name":"com.sun.identity.idsvcs.InvalidPassword"}}

1 个答案:

答案 0 :(得分:0)

解决了这个问题。目标系统正在设置响应。