java json post请求参数_JAVA发送POST请求携带JSON格式字符串参数

importorg.apache.commons.lang.StringUtils;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.StatusLine;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.HttpResponseException;importorg.apache.http.client.ResponseHandler;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importjava.io.IOException;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;public classPostUtil {/*** 发送post请求

*@paramurl 请求地址

*@paramjson json格式字符串

*@paramcontentType 这里用 "application/json"

*@return

*/

public staticString post(String url, String json, String contentType) {

HttpClientBuilder httpClientBuilder=HttpClientBuilder.create();//HttpClient

CloseableHttpClient client =httpClientBuilder.build();

client=(CloseableHttpClient) wrapClient(client);

HttpPost post= newHttpPost(url);try{

StringEntity s= new StringEntity(json, "utf-8");if(StringUtils.isBlank(contentType)) {

s.setContentType("application/json");

}

s.setContentType(contentType);

post.setEntity(s);

HttpResponse res=client.execute(post);

HttpEntity entity=res.getEntity();

String str= EntityUtils.toString(entity, "utf-8");returnstr;

}catch(Exception e) {

e.printStackTrace();

}return null;

}private staticorg.apache.http.client.HttpClient wrapClient(HttpClient base) {try{

SSLContext ctx= SSLContext.getInstance("TLSv1");

X509TrustManager tm= newX509TrustManager() {public voidcheckClientTrusted(X509Certificate[] xcs,

String string)throwsCertificateException {

}public voidcheckServerTrusted(X509Certificate[] xcs,

String string)throwsCertificateException {

}publicX509Certificate[] getAcceptedIssuers() {return null;

}

};

ctx.init(null, new TrustManager[]{tm}, null);

SSLConnectionSocketFactory sslsf= new SSLConnectionSocketFactory(ctx, new String[]{"TLSv1"}, null,

SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

CloseableHttpClient httpclient=HttpClients.custom().setSSLSocketFactory(sslsf).build();returnhttpclient;

}catch(Exception ex) {return null;

}

}public class CharsetHandler implements ResponseHandler{privateString charset;publicCharsetHandler(String charset) {this.charset =charset;

}publicString handleResponse(HttpResponse response)throwsClientProtocolException, IOException {

StatusLine statusLine=response.getStatusLine();if (statusLine.getStatusCode() >= 300) {throw newHttpResponseException(statusLine.getStatusCode(),

statusLine.getReasonPhrase());

}

HttpEntity entity=response.getEntity();if (entity != null) {if (!StringUtils.isBlank(charset)) {returnEntityUtils.toString(entity, charset);

}else{returnEntityUtils.toString(entity);

}

}else{return null;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值