akhilesh_siddhanti

Untitled

Feb 29th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8.  
  9. import javax.net.ssl.HostnameVerifier;
  10. import javax.net.ssl.HttpsURLConnection;
  11. import javax.net.ssl.SSLContext;
  12. import javax.net.ssl.SSLSession;
  13. import javax.net.ssl.TrustManager;
  14. import javax.net.ssl.X509TrustManager;
  15.  
  16.  
  17. public class Main {
  18.  
  19. private static final String my_url = "https://10.1.0.10:8090/login.xml";
  20. private static final String url2 = "https://www.google.com";
  21. private static final String url3 = "https://www.facebook.com";
  22. @SuppressWarnings("deprecation")
  23.  
  24.  
  25. public static void main(String[] args) throws IOException {
  26. String line;
  27. URL url = new URL(my_url);
  28. certificate_and_verification();//Some random certificate validation check
  29.  
  30. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  31. connection.setDoOutput(true);
  32. connection.setRequestMethod("POST");
  33. connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
  34. connection.setRequestProperty( "charset", "utf-8");
  35. connection.setUseCaches( false );
  36. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  37.  
  38. while((line = br.readLine() )!= null)
  39. {
  40. System.out.println(line);
  41.  
  42. }
  43.  
  44.  
  45.  
  46. // int responseCode = connection.getResponseCode();
  47.  
  48. /*BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  49. if (responseCode == HttpsURLConnection.HTTP_OK){
  50. System.out.println("Connection successful");
  51.  
  52. }
  53.  
  54. while((line = br.readLine() )!= null)
  55. {
  56. System.out.println(line);
  57.  
  58. }
  59. */
  60. //Let's test only for f2014866 id
  61. String POST_PARAMS = "username="+ URLEncoder.encode("f2014866")+ "&password="+URLEncoder.encode("555")+"&mode="+ 191;
  62.  
  63. OutputStream os = connection.getOutputStream();
  64. os.write(POST_PARAMS.getBytes());
  65. os.flush();
  66. os.close();
  67.  
  68. BufferedReader br1 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  69.  
  70. while((line = br1.readLine() )!= null)
  71. {
  72. System.out.println(line);
  73.  
  74. }
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. private static void certificate_and_verification() {
  83. /*Certificate testing*/
  84.  
  85. TrustManager[] trustAllCerts = new TrustManager[]{
  86. new X509TrustManager() {
  87.  
  88. public java.security.cert.X509Certificate[] getAcceptedIssuers()
  89. {
  90. return null;
  91. }
  92. public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
  93. {
  94. //No need to implement.
  95. }
  96. public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
  97. {
  98. //No need to implement.
  99. }
  100. }
  101. };
  102.  
  103. // Install the all-trusting trust manager
  104. try
  105. {
  106. SSLContext sc = SSLContext.getInstance("SSL");
  107. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  108. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  109. }
  110. catch (Exception e)
  111. {
  112. System.out.println(e);
  113. }
  114. //Installing host name verifier
  115.  
  116. // Create all-trusting host name verifier
  117. HostnameVerifier allHostsValid = new HostnameVerifier() {
  118. @Override
  119. public boolean verify(String hostname, SSLSession session) {
  120. return true; }
  121. };
  122. // Install the all-trusting host verifier
  123. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  124. /* Done */
  125. }
  126.  
  127. }
Add Comment
Please, Sign In to add comment