httpclient post json数据
所属分类 httpclient
浏览量 1397
public static String doPostBody(String url,String body) throws Exception
HttpPost http = new HttpPost(url);
http.setEntity(new StringEntity(body,ContentType.APPLICATION_JSON));
//http.setHeader("Content-Type", "text/html;charset=UTF-8");
http.setHeader("Connection", "keep-alive");
CloseableHttpResponse response = httpClient.execute(http);
StatusLine sl = response.getStatusLine();
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, CommConst.UTF8);
new StringEntity(body,ContentType.APPLICATION_JSON)
HttpEntity entity = response.getEntity();
EntityUtils.toString(entity, CommConst.UTF8);
springboot 服务端接口
@RequestMapping("/echo")
public UserDO echo(@RequestBody UserDO user) {
return user;
}
mvn spring-boot:run -Dstart-class=demo.DemoApp
默认端口 8090
指定端口 -Dspring-boot.run.arguments=--server.port=8080
HttpClientUtil 完整代码
https://gitee.com/dyyx/hellocode/blob/master/project/jpress/src/main/java/dyyx/util/HttpClientUtil.java
客户端测试代码 HttpClientUtilPostBodyTest
https://gitee.com/dyyx/hellocode/blob/master/project/jpress/src/test/java/dyyx/util/HttpClientUtilPostBodyTest.java
springboot 服务端工程代码
https://gitee.com/dyyx/springboothello
Maven命令行直接运行SpringBoot项目
上一篇
下一篇
MySQL事务机制
秒杀系统设计思路
Shenandoah垃圾收集器
jmx信息获取
JMX之Jolokia使用
java类加载知识点