概述
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数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复