用httclient发送请求

代码自己看,看不懂的私信我

package com.dst.slms.app.utils;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

/**
 * 
 * @author chenmin
 *
 */
public class HttpClientUtil {

	public static CloseableHttpClient createSSLClient(){
		SSLContext sslContext = null;
		
		try {
			sslContext = new SSLContextBuilder().loadTrustMaterial(null,new TrustStrategy(){

				@Override
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					//通过所有证书
					return true;
				}
				
			}).build();
			
			SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier(){

				@Override
				public boolean verify(String arg0, SSLSession arg1) {
					// 不用验证hostname
					return true;
				}
				
			});
			//如果异常了,创建普通的client
			return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return HttpClients.createDefault();
	}
}

package com.dst.slms.app.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;


public class HttpUtil {

	static Logger log = LoggerFactory.getLogger(HttpUtil.class);
	
	public static String httpPost(String url, Object data) {
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		CloseableHttpClient httpClient = HttpClientUtil.createSSLClient();
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
		httpPost.setConfig(requestConfig);
		httpPost.addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());

		try {
			StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), "utf-8");
			httpPost.setEntity(requestEntity);
			response = httpClient.execute(httpPost, new BasicHttpContext());
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity == null) {
					return null;
				}

				String resultStr = EntityUtils.toString(entity, "utf-8");
				String var10 = resultStr;
				return var10;
			}

			log.error("request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + url);
		} catch (IOException var20) {
			log.error("request url=" + url + ", exception, msg=" + var20.getMessage());
			var20.printStackTrace();
			return null;
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException var19) {
					var19.printStackTrace();
				}
			}

		}

		return null;
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值