java使用httpcomponents post发送json数据
一、适用场景
当我们向第三方系统提交数据的时候,需要调用第三方系统提供的接口。不同的系统提供的接口也不一样,有的是SOAP Webservice、RESTful Webservice 或其他的。当使用的是RESTful webservice的时候,就可以使用httpcomponents组件来完成调用。
如我们需要发起post请求,并将数据转成json格式,设置到post请求中并提交。
url:"http://www.xxxxx.com/message"
json数据格式 {"name":"zhangsan", "age":20, "gender": "mail"} // 一个用户的基本信息
二、实例代码
- package com.demo.test;
- import java.io.IOException;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- public class Test {
- public static String sendInfo(String sendurl, String data) {
- CloseableHttpClient client = HttpClients.createDefault();
- HttpPost post = new HttpPost(sendurl);
- StringEntity myEntity = new StringEntity(data,
- ContentType.APPLICATION_JSON);// 构造请求数据
- post.setEntity(myEntity);// 设置请求体
- String responseContent = null; // 响应内容
- CloseableHttpResponse response = null;
- try {
- response = client.execute(post);
- if (response.getStatusLine().getStatusCode() == 200) {
- HttpEntity entity = response.getEntity();
- responseContent = EntityUtils.toString(entity, "UTF-8");
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (response != null)
- response.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (client != null)
- client.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return responseContent;
- }
- public static void main(String[] args) {
- String json = "{\"name\":\"zhangsan\", \"age\":20, \"gender\": \"mail\"} ";
- String result = sendInfo("http://www.xxxxx.com/message", json);
- System.out.println(result);
- }
- }
发送请求之后,后台会打印返回的信息。
java使用httpcomponents post发送json数据的更多相关文章
- java模拟post请求发送json数据
import com.alibaba.fastjson.JSONObject; import org.apache.http.client.methods.CloseableHttpResponse; ...
- JSON的简单使用_向前台发送JSON数据
转自:http://www.cnblogs.com/digdeep/p/5574366.html 1.前台页面 <%@ page language="java" conten ...
- SpringMVC客户端发送json数据时报400错误
当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 谈谈Java利用原始HttpURLConnection发送POST数据
这篇文章主要给大家介绍java利用原始httpUrlConnection发送post数据,设计到httpUrlConnection类的相关知识,感兴趣的朋友跟着小编一起学习吧 URLConnectio ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- perl post发送json数据
sub wx_init { #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=- ...
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- java 判断String字符串是不是json数据
java 判断String字符串是不是json数据 CreationTime--2018年8月24日18点23分 Author:Marydon JSONObject jo = null; try ...
随机推荐
- ExtendHelper
public static class ExtendHelper { /// <summary> /// 检查当前字符串是否符合某种格式 /// </summary> /// ...
- C#字符串的恒定性
string str1="aa"; string str2="aa"; str1,str2,变量所指向的堆空间的地址是一样的.栈空间的内容是不一样的. //ne ...
- 2015年百度之星初赛(1) --- B 找连续数
找连续数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 组合数学 - 母函数的运用 --- hdu 1709 :The Balance
The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- C#设计模式——模板方法(Template Method)
一.概述在软件开发中,对某一项操作往往有固定的算法结构,而具体的子步骤会因为不同的需要而有所不同.如何可以在稳定算法结构的同时来灵活应对子步骤变化的需求呢?二.模板方法模板方法是一种常见的设计模式,它 ...
- css 布局absolute与relative的区别
absolute:当使用时,表示在文档流中没有实际存在位置(浮动),在不设置任何方位值时,只能按兵不动,当设置了方位值之后,会紧接着去寻找距离最近的能够将它包含住的父级元素,然后进行定位. relat ...
- C#一个方法返回多个值
示例代码: static void Main(string[] args) { //声明 int value; string strOutValue; //调用函数 //函数的参数有两个返回的值 Re ...
- OAuth2.0 基础概述
web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...
- DistributedCache小记
一.DistributedCache简介 DistributedCache是hadoop框架提供的一种机制,可以将job指定的文件,在job执行前,先行分发到task执行的机器上,并有相关机制对cac ...
- java LinkedBlockingQueue和ConcurrentLinkedQueue的区别
实现上看,两者都继承于AbstractQueue,但是ConcurrentLinkedQueue实现了Queue,而LinkedBlockingQueue实现了BlockingQueue,Blocki ...