获取HTTP / 1.1 400错误请求

时间:2017-12-22 14:14:21

标签: java http service

这是我的代码。我收到HTTP / 1.1 400 Bad Request。不知道我在这里做错了什么。

try {
                //client = new DefaultHttpClient();
                client=HttpClientBuilder.create().build();
                get = new HttpGet(queryURL.toString());
                HttpResponse response = client.execute(get);
                if(HttpStatus.SC_OK!=response.getStatusLine().getStatusCode()) {
                    throw new ServiceException(this, response.getStatusLine().getStatusCode()+"", response.getStatusLine().toString());
                }
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                StringBuffer json = new StringBuffer();
                String line = "";
                while ((line = rd.readLine()) != null) {
                    json.append(line);
                }

错误消息。

ServiceException:HTTP / 1.1 400 Bad Request

1 个答案:

答案 0 :(得分:0)

首先要查明Bad Request的原因,您应该看到响应文字。您可以写如下:

client=HttpClientBuilder.create().build();
get = new HttpGet(queryURL.toString());
HttpResponse response = client.execute(get);

 BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            StringBuilder responseAsString = new StringBuilder();
            String line = "";
            while ((line = rd.readLine()) != null) {
                responseAsString.append(line);
            }

if(HttpStatus.SC_OK!=response.getStatusLine().getStatusCode()) {
      throw new ServiceException(this, response.getStatusLine().getStatusCode()+"", response.getStatusLine().toString() + ", cause: " + responseAsString);
}