我是靠谱客的博主 默默乌冬面,这篇文章主要介绍java httpcomponents_java使用httpcomponents post发送json数据,现在分享给大家,希望可以做个参考。

package com.gaojig;

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 = "{"token":"5b112f231ac1c644f17526f4f809b25c","name":"马涛","cardno": "362502199307093824"}";

String result = sendInfo("http://service.gaojig.com/api/jkzj/VerifyIdcard", json);

System.out.println(result);

}

}

最后

以上就是默默乌冬面最近收集整理的关于java httpcomponents_java使用httpcomponents post发送json数据的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(151)

评论列表共有 0 条评论

立即
投稿
返回
顶部