String userId = request.getParameter("userId");
HttpClient http = new HttpClient();
String returnJson="";
try {
PostMethod method = new PostMethod(url);
method.addParameter("userId", userId); //传递参数
http.executeMethod(method);
returnJson = method.getResponseBodyAsString(); //返回结果
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Charset", "UTF-8");
PrintWriter out = response.getWriter();
out.print(returnJson);
out.flush();
out.close();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient http = new HttpClient();
String returnJson="";
try {
PostMethod method = new PostMethod(url);
method.addParameter("userId", userId); //传递参数
http.executeMethod(method);
returnJson = method.getResponseBodyAsString(); //返回结果
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Charset", "UTF-8");
PrintWriter out = response.getWriter();
out.print(returnJson);
out.flush();
out.close();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
本文介绍了一种使用Java中的HttpClient发起POST请求的方法,并展示了如何设置请求参数及处理响应结果的具体步骤。
1万+

被折叠的 条评论
为什么被折叠?



