利用HttpClient进行post请求的工具类,访问第三方接口HTTPs

发布时间:2020-07-11 16:49:03 作者:ckllf
来源:网络 阅读:859

  原代码:

  package com.landray.kmss.util.web;

  import java.io.IOException;

  import java.util.ArrayList;

  import java.util.Iterator;

  import java.util.List;

  import java.util.Map;

  import java.util.ResourceBundle;

  import java.util.Map.Entry;

  import javax.servlet.ServletException;

  import javax.servlet.http.HttpServlet;

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  import org.apache.http.HttpEntity;

  import org.apache.http.HttpResponse;

  import org.apache.http.NameValuePair;

  import org.apache.http.client.HttpClient;

  import org.apache.http.client.entity.UrlEncodedFormEntity;

  import org.apache.http.client.methods.HttpPost;

  import org.apache.http.impl.client.CloseableHttpClient;

  import org.apache.http.message.BasicNameValuePair;

  import org.apache.http.util.EntityUtils;

  import net.sf.json.JSONObject;

  /*

  * 利用HttpClient进行post请求的工具类 访问第三方接口HTTPs

  */

  public class HttpClientUtil extends HttpServlet {

  /**

  *

  */

  private static final long serialVersionUID = 1L;

  /*

  * private static ResourceBundle lStrings =

  * ResourceBundle.getBundle("javax.servlet.http.LocalStrings");

  *

  * public void doPost(HttpServletRequest req, HttpServletResponse resp)

  * throws ServletException, IOException { String protocol =

  * req.getProtocol();

  *

  * String msg = lStrings.getString("http.method_post_not_supported"); if

  * (protocol.endsWith("1.1")) { resp.sendError(405, msg); } else {

  * resp.sendError(400, msg); }

  *

  * }

  */

  public static JSONObject doPostUrl(String url, Map map, String charset) {

  JSONObject jsonObject = null;

  CloseableHttpClient httpClient = null; // HttpsURLConnection

  HttpPost httpPost = null;

  String result = null;

  try {

  httpClient = new SSLClient();

  httpPost = new HttpPost(url);

  System.setProperty("sun.net.client.defaultReadTimeout", "5000");

  System.setProperty("jsse.enableSNIExtension", "false");

  // 设置参数

  List list = new ArrayList();

  Iterator iterator = map.entrySet().iterator();

  while (iterator.hasNext()) {

  Entry elem = (Entry) iterator.next();

  list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));

  }

  if (list.size() > 0) {

  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);

  httpPost.setEntity(entity);

  }郑州妇科医院 http://www.120zzkd.com/

  HttpResponse response = httpClient.execute(httpPost);

  if (response != null) {

  System.out.println("123");

  HttpEntity resEntity = response.getEntity();

  if (resEntity != null) {

  result = EntityUtils.toString(resEntity, charset);

  jsonObject = JSONObject.fromObject(result);

  }

  }

  } catch (Exception ex) {

  ex.printStackTrace();

  } finally {

  try {

  httpClient.close();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  return jsonObject;

  }

  }

  SSLClient代码:

  package com.landray.kmss.util.web;

  import java.security.cert.CertificateException;

  import java.security.cert.X509Certificate;

  import javax.net.ssl.SSLContext;

  import javax.net.ssl.TrustManager;

  import javax.net.ssl.X509TrustManager;

  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;

  //用于进行Https请求的HttpClient

  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));

  }

  }

推荐阅读:
  1. 如何屏蔽https网站,禁止https网站的访问?
  2. java + httpclient +post请求的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 第三

上一篇:如何使用php中的变量__line__

下一篇:nginx和tomcat有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》