程序员:HttpClient进行post请求的工具类,访问第三方接口HTTPS

HTTPS

(英语:Hypertext Transfer Protocol Secure,缩写:HTTPS,常称为HTTP over TLS,HTTP over SSL或HTTP Secure)

是一种通过计算机网络进行安全通信的传输协议。HTTPS经由HTTP进行通信,但利用SSL/TLS来加密数据包。HTTPS开发的主要目的,是提供对网站服务器的身份认证,保护交换数据的隐私与完整性。这个协议由网景公司(Netscape)在1994年首次提出,随后扩展到互联网上。

简单来说,HTTPS 是 HTTP 的安全版,是使用 SSL/TLS 加密的 HTTP 协议。通过 TLS/SSL 协议的的身份验证、信息加密和完整性校验的功能,从而避免信息窃听、信息篡改和信息劫持的风险。

HTTPS 提供了加密 (Encryption)、认证 (Verification)、鉴定 (Identification) 三种功能。如下的解释中,假设是张三和李四在通讯。

  • 私密性(Confidentiality/Privacy):
  • 也就是提供信息加密,保证数据传输的安全;保证信息只有张三和李四知道,而不会被窃听。
  • 可信性(Authentication):
  • 身份验证,主要是服务器端的,确认网站的真实性,有些银行也会对客户端进行认证;用来证明李四就是李四。
  • 完整性(Message Integrity):
  • 保证信息传输过程中的完整性,防止被修改;李四接收到的消息就是张三发送的。

HTTPS就是在应用层和传输层中间加了一道验证的门槛以保证数据安全

程序员:HttpClient进行post请求的工具类,访问第三方接口HTTPS

上原代码

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<String, String> 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<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) {

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