Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 HttpClient:无法读取post请求中的json数据_Java_Apache Httpclient 4.x - Fatal编程技术网

Java HttpClient:无法读取post请求中的json数据

Java HttpClient:无法读取post请求中的json数据,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,这里我使用HttpClient发布JSON数据。但我无法读取其他应用程序上的数据。当我执行request.getParameter(“用户名”)时,它返回null。我的两个应用程序都部署在同一台服务器上。请告诉我我做错了什么。多谢各位 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Strin

这里我使用HttpClient发布JSON数据。但我无法读取其他应用程序上的数据。当我执行
request.getParameter(“用户名”)
时,它返回null。我的两个应用程序都部署在同一台服务器上。请告诉我我做错了什么。多谢各位

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost("http://localhost:8080/AuthenticationService/UserIdentificationServlet");
        postRequest.setHeader("Content-type", "application/json");

        StringEntity input = new StringEntity("{\"username\":\""+username+"\"}");
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse postResponse = httpClient.execute(postRequest);

        BufferedReader br = new BufferedReader(new InputStreamReader((postResponse.getEntity().getContent())));
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        httpClient.getConnectionManager().shutdown();
    }

如果要使用request.getParameter,则必须以URL编码的格式发布数据

//this example from apache httpcomponents doc
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
//此示例来自ApacheHttpComponents文档
List formparams=new ArrayList();
formparams.add(新的BasicNameValuePair(“param1”、“value1”);
formparams.add(新的BasicNameValuePair(“param2”、“value2”);
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(formparams,“UTF-8”);
HttpPost HttpPost=新的HttpPost(“http://localhost/handler.do");
httppost.setEntity(实体);

您需要了解ie表单请求参数(
HttpServletRequest#getParameter()
)和请求主体为
json
HttpServletRequest#getInputStream()
)的
应用程序/json
之间的区别。