我是靠谱客的博主 默默乌冬面,最近开发中收集的这篇文章主要介绍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 httpcomponents_java使用httpcomponents post发送json数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部