zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

httpclient4.3x的ssl请求工具类详解编程语言

SSL工具编程语言 详解 请求
2023-06-13 09:20:29 时间
public static String doPost(String url, Map String, String map, String charset) throws Exception { HttpClient httpClient = null; HttpPost httpPost = null; String result = null; httpPost = new HttpPost(url); 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); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); //设置参数 List NameValuePair list = new ArrayList NameValuePair Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry String, String elem = (Entry String, String ) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); if (list.size() 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); return result; }

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10485.html

cjava