我是靠谱客的博主 开心小蚂蚁,最近开发中收集的这篇文章主要介绍使用httpclient发送post请求servlet服务器,servlet请求另一个服务器得到数据返回给httpclient实现思路,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
使用httpclient发送post请求到服务器端,服务器端在请求另一个服务器得到数据返回给httpclient
实现思路
1、新建两个Web Appilcation项目模拟两个服务器
分别为服务器端1和服务器端2
2、建立一个httpclient模拟发送post请求
3、服务器1接收到httpclient 发来的请求后向另一个服务器2发起请求
4、服务器1接收到servlet发来的请求,并响应服务器1。
5、服务器1再将服务器2响应的数据发送给httclient
详细代码如下
httpclient发送post请求给服务器1 代码:
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 描述:
*
* @author BZMing
* @create 2019-08-12
10:44
*/
public class Client {
public static void main(String[] args) throws IOException {
//创建httpClient请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//请求地址
String url = "http://localhost:8888/demo";
//创建httpPost
HttpPost httpPost = new HttpPost(url);
//创建 参数集合
List<NameValuePair> params = new ArrayList<NameValuePair>();
//添加参数
params.add(new BasicNameValuePair("msg" , "aaaaaaaaaaaaaaa"));
//将参数集合添加到httpPost
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
// 响应
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器1接受httpclient发来的请求,对数据进行加工处理,并请求另一个服务器2:
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
* 描述:
*
* @author BZMing
* @create 2019-08-12
9:11
*/
@WebServlet("/demo")
public class servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
//接收数据
BufferedReader reader = req.getReader();
String json = reader.readLine();
System.out.println(json);
reader.close();
/**
* 将接收到的数据进行加密处理
(加密前 json,加密后
endjson)
*/
String endjson="demo发来的"+json+"数据!";
//创建httpClient请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//请求地址
String url = "http://localhost:6666/test";
//创建http Post请求
HttpPost httpPost = new HttpPost(url);
//创建 参数集合
List<NameValuePair> params = new ArrayList<NameValuePair>();
//添加参数
params.add(new BasicNameValuePair("str" , endjson));
//将abcdefg改为 加密处理后的字符串
endjson
//将参数集合添加到httpPost
httpPost.setEntity(new UrlEncodedFormEntity(params));
//设置请求的格式
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
CloseableHttpResponse response = null;
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
//httpclient发送请求后,从响应实体中获取对方返回的数据
String strmsg = EntityUtils.toString(responseEntity);
System.out.println(strmsg);
/**
* 将收到响应的数据进行处理
处理前 strmsg
处理后后sendmsg
*/
String sendString ="处理后的"+strmsg+"数据";
//向最开始的请求返回数据
endmsg
PrintWriter out = resp.getWriter();
out.write(sendString);
//释放资源。
if (reader!=null){
reader.close();
}
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
if (out!=null){
out.close();
}
}
}
服务器2接受到服务器1发来的请求,并返回数据
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 描述:
*
* @author BZMing
* @create 2019-08-12
10:42
*/
@WebServlet("/test")
public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
/**
* 接收数据
*/
BufferedReader reader = req.getReader();
String json = reader.readLine();
System.out.println(“发送过来的数据是”+json);
reader.close();
/**
* 返回数据
*/
PrintWriter out = resp.getWriter();
String sendString;
sendString="asdcfe";
//此服务器要返回的数据
out.write(sendString);
out.close();
}
}
最后
以上就是开心小蚂蚁为你收集整理的使用httpclient发送post请求servlet服务器,servlet请求另一个服务器得到数据返回给httpclient实现思路的全部内容,希望文章能够帮你解决使用httpclient发送post请求servlet服务器,servlet请求另一个服务器得到数据返回给httpclient实现思路所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复