HttpClient包是一个优秀的Http请求的开源jar。
本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求。
但是由于项目的需要快速的实现,以下代码还可能会有点过时,但是要是可行的,并且相对比较简单。
1.实现一个SSLClient的客户端
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class SSLClient extends DefaultHttpClient { public SSLClient() throws Exception{ super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } }
2.HttpClientUtils工具类
import com.google.common.base.Charsets; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpClientUtil { public static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); public static String doPost(String url, String json) { HttpClient httpClient; HttpPost httpPost; String result = null; try { httpClient = new SSLClient(); httpPost = new HttpPost(url); StringEntity entity = new StringEntity(json, Charsets.UTF_8);//解决中文乱码问题 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, Charsets.UTF_8); } } } catch (Exception e) { logger.error("调用访问https工具类, 抛错:", e); } return result; } }
http://www.cnblogs.com/stevendes/p/5971706.html
相关推荐
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
利用HttpClient发送HTTP请求 利用HttpClient发送HTTP请求
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
其中包含项目post-demo定义接口;包含另一个项目httpclient-demo使用工具类发送post请求调用接口。详细内容查看博客 httpclient发送post请求
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
httpclient post方式发送json格式数据,需要的相关包。
通过HTTPClient发起HTTPS GET请求(返回值为字符串)、POST请求(返回值为JSONOBJECT),需引入fastJson的jar包
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
httpclient的用法,发送get请求和post请求,设置header
解决证书错误,利用httpClient发送https请求,内容格式为XML,另外包中有sha1加密,需要的包,速速下载
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
JAVA发送HttpClient请求及接收请求结果过程
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
httpclient发送post请求
NULL 博文链接:https://daoshud1.iteye.com/blog/1847378
esp8266 发起https 请求,请求 https 的api 等等,整个项目工程
采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解