我正在尝试使用httpClient发送JSON对象,但在DoPost中,“HttpServletRequest请求”为空,它没有参数。
另外,我尝试使用Postman chrome app向同一个类发出请求,并且它还有空的“HttpServletRequest请求”。
这里是如何提出请求:
public void CamposPost(){
try{
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(this.url);
StringEntity postingString = new StringEntity(this.JSONObject.toString());
post.setHeader("Content-type", "application/json");
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
this.respond=responseString;
}
catch(Exception ex){
ex.printStackTrace();
}
}
以下代码是DoPost代码
package com.itc.polux.request;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class post extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> parameterNames = new ArrayList<String>(request.getParameterMap().keySet());
//request.getParameterNames();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(request.getParameter("json"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
List<String> parameterNames = new ArrayList<String>(request.getParameterMap().keySet());
//request.getParameterNames();
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println(request.getParameter("json"));
}
}