HttpPost返回一个空体

时间:2016-02-29 15:39:37

标签: java http-post httpclient

我有下面的代码,我的问题是response.status是200(没问题),我的响应体是空的,它应该返回一个json响应。问题的长度可能是?:`

    HttpPost post = new HttpPost("http://localhost:8080/rest/login");
    httpClient = HttpClients.createDefault();

    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    NameValuePair pair = new BasicNameValuePair("token",token);
    formParams.add(pair);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams);
    post.setEntity(entity);


    HttpResponse response = httpClient.execute(post);
    this.code = response.getStatusLine().getStatusCode();
    InputStream body = response.getEntity().getContent();
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));`

1 个答案:

答案 0 :(得分:0)

我有类似的问题。在我的情况下,它也很奇怪,当url包含IP而不是主机名时,它起作用(我得到了非空的响应)。

但是,在我的情况下设置netscape cookie规范有帮助。

httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom()
            .setCookieSpec(CookieSpecs.NETSCAPE)
            .build());