Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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 apache http客户端发送url编码的post请求_Java_Apache_Apache Httpclient 4.x_Dropwizard - Fatal编程技术网

Java apache http客户端发送url编码的post请求

Java apache http客户端发送url编码的post请求,java,apache,apache-httpclient-4.x,dropwizard,Java,Apache,Apache Httpclient 4.x,Dropwizard,我有一个dropwizard服务,在该服务中我实现了一个post请求,该请求使用应用程序\u表单\u URL编码的媒体类型并使用@FormParam注释 然后在我的客户端中,我使用Apache HttpClient发出如下post请求: public void sendPost(String path, JsonObject params) throws Exception { String url = "http://" + TS_API_HOST + ":"

我有一个dropwizard服务,在该服务中我实现了一个post请求,该请求使用应用程序\u表单\u URL编码的媒体类型并使用@FormParam注释

然后在我的客户端中,我使用Apache HttpClient发出如下post请求:

public void sendPost(String path, JsonObject params) throws Exception {

                String url = "http://" + TS_API_HOST + ":" + TS_API_PORT + "/" + path;

                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();

                Iterator<String> keys = params.keySet().iterator();
                while(keys.hasNext()){
                    String currentKey = keys.next();
                    nvps.add(new BasicNameValuePair(currentKey, params.get(currentKey).toString()));
                }
                System.out.println(nvps.toString());
                httpPost.setEntity(new UrlEncodedFormEntity(nvps));

                CloseableHttpResponse response = httpClient.execute(httpPost);

                try {
                    System.out.println(response.getStatusLine());
                    HttpEntity entity2 = response.getEntity();
                    // do something useful with the response body
                    // and ensure it is fully consumed
                    EntityUtils.consume(entity2);
                } finally {
                    response.close();
                }

            }
public void sendPost(字符串路径,JsonObject参数)引发异常{
String url=“http://”+TS_API_HOST+:“+TS_API_PORT+”/“+path;
CloseableHttpClient httpClient=HttpClients.createDefault();
HttpPost HttpPost=新的HttpPost(url);
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
List nvps=new ArrayList();
迭代器键=params.keySet().Iterator();
while(keys.hasNext()){
字符串currentKey=keys.next();
添加(新的BasicNameValuePair(currentKey,params.get(currentKey).toString());
}
System.out.println(nvps.toString());
setEntity(新的UrlEncodedFormEntity(nvps));
CloseableHttpResponse response=httpClient.execute(httpPost);
试一试{
System.out.println(response.getStatusLine());
HttpEntity entity2=response.getEntity();
//对响应体执行一些有用的操作
//并确保它被完全消耗
EntityUtils.consume(entity2);
}最后{
response.close();
}
}
我传递的url和参数是正确的,但我一直收到400个错误请求作为响应


在Postman中,它工作得非常好…

有一个发送url编码请求的示例:有一个发送url编码请求的示例: