Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Enable SSL connection for Jsoup
  1. import org.jsoup.Connection;
  2. import org.jsoup.Jsoup;
  3.  
  4. import javax.net.ssl.*;
  5. import java.io.IOException;
  6. import java.security.KeyManagementException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.security.SecureRandom;
  9. import java.security.cert.CertificateException;
  10. import java.security.cert.X509Certificate;
  11.  
  12. public class TaipeiWater {
  13.     
  14.     final static String requestUrl = "https://somewhere.com/target.jsp";
  15.  
  16.     public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException {
  17.         HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
  18.             public boolean verify(String hostname, SSLSession session) {
  19.                 return true;
  20.             }
  21.         });
  22.  
  23.         SSLContext context = SSLContext.getInstance("TLS");
  24.         context.init(null, new X509TrustManager[]{new X509TrustManager() {
  25.             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  26.             }
  27.  
  28.             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  29.             }
  30.  
  31.             public X509Certificate[] getAcceptedIssuers() {
  32.                 return new X509Certificate[0];
  33.             }
  34.         }}, new SecureRandom());
  35.         HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
  36.     }
  37.  
  38.     public static void main(String[] args) throws IOException, NoSuchAlgorithmException, KeyManagementException {
  39.         String bigno = "S";
  40.         String midno = "11";
  41.         String useno = "111111";
  42.         String chkno = "1";
  43.  
  44.         enableSSLSocket();
  45.  
  46.         Connection.Response response = Jsoup.connect(requestUrl)
  47.                 .data("bigno", bigno)
  48.                 .data("midno", midno)
  49.                 .data("useno", useno)
  50.                 .data("chkno", chkno)
  51.                 .userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0")
  52.                 .method(Connection.Method.POST)
  53.                 .execute();
  54.  
  55.         switch (response.statusCode()) {
  56.             case 200:
  57.                 doProcess(response.parse());
  58.                 break;
  59.             default:
  60.                 
  61.                 break;
  62.         }
  63.     }
  64.  
  65.     public static void doProcess(Document document){
  66.         // do something...
  67.     }
  68. }