0
在这里,我使用HttpClient发布JSON数据。但我无法读取其他应用程序上的数据。当我做request.getParameter("username")
时,它返回null。我的应用程序都部署在同一台服务器上。请告诉我我做错了什么。谢谢Java HttpClient:无法在post请求中读取json数据
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();
}
你需要了解之间[应用程序/ x-WWW窗体-urlencoded](http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart的差(''HttpServletRequest#getParameter()')和具有'json'('HttpServletRequest#getInputStream()')请求体的'application/json'。 – 2013-02-16 05:13:46