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... Copy/Delete/Permission directory using System; using System.IO; using System.Security.AccessControl; namespace Bruce.Lib {     public class DirHelper     {         /// <...
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. }
Register preference change event with PreferenceFragment
  1. public class DevicePreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
  2.     @Override
  3.     public void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         addPreferencesFromResource(R.xml.preference);
  6.     }
  7.  
  8.     @Override
  9.     public void onResume() {
  10.         super.onResume();
  11.         getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  12.  
  13.     }
  14.  
  15.     @Override
  16.     public void onPause() {
  17.         getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  18.         super.onPause();
  19.     }
  20.  
  21.     @Override
  22.     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  23.         Log.d(MainActivity.TAG, String.format("Preference '%s' has been changed.", key));
  24.         if ("pref_track_interval".equals(key)) {
  25.             // do something
  26.         }
  27.     }
  28. }
Set remote respository for Intellij IDEA git integration
  1. Create a new project
  2. VCS -> Import into Version Control -> Create Git Repository... , then select the project path
  3. Open project path, find the config file at hidden subfolder .git
    Edit config file, add following contents at end:
    1. [remote "origin"]
    2. url = http://www.yourgitserver.com/repository.git
    3. fetch = +refs/heads/*:refs/remotes/origin/*
  4. File -> Synchronize
  5. That it! Try commit and push your project