Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我收到HTTP 400错误请求_Java_Post_Httpclient - Fatal编程技术网

Java 为什么我收到HTTP 400错误请求

Java 为什么我收到HTTP 400错误请求,java,post,httpclient,Java,Post,Httpclient,我正在使用HTTP客户端(从中复制的代码)发送post请求。我一直试图用它来查找大量的邮政编码,但失败了。据我所知,我应该以以下JSON格式向发送post请求:{“postcodes”:[“OX49 5NU”、“M32 0JG”、“NE30 1DP”]}但我总是收到HTTP响应代码400。 我在下面包含了我的代码。请告诉我我做错了什么? 谢谢 private void sendPost()引发异常{ 字符串url=”http://api.postcodes.io/postcodes"; Http

我正在使用HTTP客户端(从中复制的代码)发送post请求。我一直试图用它来查找大量的邮政编码,但失败了。据我所知,我应该以以下JSON格式向发送post请求:
{“postcodes”:[“OX49 5NU”、“M32 0JG”、“NE30 1DP”]}
但我总是收到HTTP响应代码400。 我在下面包含了我的代码。请告诉我我做错了什么? 谢谢

private void sendPost()引发异常{
字符串url=”http://api.postcodes.io/postcodes";
HttpClient client=HttpClientBuilder.create().build();
HttpPost=新的HttpPost(url);
List urlParameters=new ArrayList();
添加(新的BasicNameValuePair(“邮政编码”,“OX49 5NU\”,“M32 0JG\”,“NE30 1DP\”));
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
System.out.println(“响应代码:
+response.getStatusLine().getStatusCode());
System.out.println(“原因:”
+response.getStatusLine().getReasonPhrase());
BufferedReader br=新的BufferedReader(
新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
字符串行=”;
而((line=br.readLine())!=null){
结果。追加(行);
}
br.close();
System.out.println(result.toString());
}
Jon Skeet是对的(像往常一样,我可能会补充),您基本上是在发送表单,它默认为表单url编码。 您可以尝试以下方式:

String jsonString = "{\"postcodes\" : [\"OX49 5NU\", \"M32 0JG\", \"NE30 1DP\"]}";
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
post.setEntity(entity);

这是可行的,不推荐使用HTTP.UTF_8:

String url = "http://api.postcodes.io/postcodes";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);

StringEntity params =new StringEntity("{\"postcodes\" : [\"OX49 5NU\", \"M32 0JG\", \"NE30 1DP\"]}");
post.addHeader("Content-Type", "application/json");
post.setEntity(params);

我怀疑您目前使用的post表单将以表单url编码的参数集的形式发送此内容,而您只想发布一个包含JSON的主体。当然,我可能错了,但我怀疑您根本不想要UrlEncodedFormEntity。我同意您的看法,我现在使用的是StringEntity。我尝试过这个
String jsonString=“{\'postcodes\”:[\'OX49 5NU\',\'M32 0JG\',\'NE30 1DP\'”];StringEntity=新的StringEntity(requestJSON.toString(),“UTF-8”);后设实体(实体)它与您的代码尽可能接近。我仍然得到了400,但这次有了更详细的解释:{“状态”:400,“错误”:“提交的JSON无效。您需要提交一个带有邮政编码或地理位置对象数组的JSON对象”}回答得很好,问题解决了!我错过了添加标题,我没有想到。非常感谢
String url = "http://api.postcodes.io/postcodes";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);

StringEntity params =new StringEntity("{\"postcodes\" : [\"OX49 5NU\", \"M32 0JG\", \"NE30 1DP\"]}");
post.addHeader("Content-Type", "application/json");
post.setEntity(params);