获取 HTTP/1.1 400 错误请求
Posted
技术标签:
【中文标题】获取 HTTP/1.1 400 错误请求【英文标题】:getting HTTP/1.1 400 Bad Request 【发布时间】:2017-12-22 14:14:20 【问题描述】:这是我的代码。我收到 HTTP/1.1 400 错误请求。不知道我这里是不是做错了什么。
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 错误请求
【问题讨论】:
也许你应该检查请求中使用的queryURL
。
检查设置'Content-Type'
【参考方案1】:
要找出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);
【讨论】:
以上是关于获取 HTTP/1.1 400 错误请求的主要内容,如果未能解决你的问题,请参考以下文章
逻辑错误的 HTTP 400(错误请求),不是格式错误的请求语法
Jetty 在格式错误的 HTTP POST 标头上返回“HTTP/1.1 400 Bad Request”。这是预期的吗?