网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

原代码:

创新互联公司是一家专业提供清水河企业网站建设,专注与网站制作、成都网站设计、H5技术、小程序制作等业务。10年已为清水河众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

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

}

}


分享名称:利用HttpClient进行post请求的工具类,访问第三方接口HTTPs
文章源于:http://bjjierui.cn/article/ppjeci.html

其他资讯