利用HttpClient进行post请求的工具类,访问第三方接口HTTPs-成都快上网建站

利用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
当前URL:http://kswjz.com/article/ppjeci.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流