当前位置:   article > 正文

java 通过 SSL/TLS 发送 Post 请求(无参数)

ssl/tls协议上post数据java
/** *  */package com.company.common.mail;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ConnectException;import java.net.URL;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSession;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import com.company.TestLogger;public class Test {	private static TestLogger logger = TestLogger.getLogger(TrustSSL.class);	private static class TrustAnyTrustManager implements X509TrustManager {		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {		}		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {		}		public X509Certificate[] getAcceptedIssuers() {			return new X509Certificate[] {};		}	}	private static class TrustAnyHostnameVerifier implements HostnameVerifier {		public boolean verify(String hostname, SSLSession session) {			return true;		}	}	public static String sendHttps(String url) throws Exception {		InputStream in = null;		OutputStream out = null;		String returnValue = "";		try {			//SSLContext sc = SSLContext.getInstance("SSL");			System.setProperty("https.protocols", "TLSv1");		    SSLContext sc = SSLContext.getInstance("TLSv1");			sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());			URL console = new URL(url);			HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();			conn.setSSLSocketFactory(sc.getSocketFactory());			conn.setHostnameVerifier(new TrustAnyHostnameVerifier());			conn.connect();			InputStream is = conn.getInputStream();			DataInputStream indata = new DataInputStream(is);			returnValue = indata.readLine();						conn.disconnect();		} catch (ConnectException e) {			logger.logError(e.getMessage());			logger.logError(e);			throw e;		} catch (IOException e) {			logger.logError(e.getMessage());			logger.logError(e);			throw e;		} finally {			try {				in.close();			} catch (Exception e) {			}			try {				out.close();			} catch (Exception e) {			}		}		return returnValue;	}	public static void main(String[] args) throws Exception {		System.out.println(sendHttps("https://test.com/get.do"));	}}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/118256
推荐阅读
相关标签
  

闽ICP备14008679号