Advertisement
Guest User

http

a guest
Feb 25th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. private static HttpResponse performPostRequest(String requestURL, HashMap<String, String> postDataParams) {
  2. URL url;
  3. String response = "";
  4. HttpsURLConnection conn = null;
  5. try {
  6. // Log.w(TAG, "performPostRequest: " + requestURL);
  7.  
  8. url = new URL(requestURL);
  9.  
  10. conn = (HttpsURLConnection) url.openConnection();
  11. //conn.setSSLSocketFactory(new TLSSocketFactory());
  12. conn.setSSLSocketFactory(getSSLSocketFactory());
  13. conn.setReadTimeout(15000);
  14. conn.setConnectTimeout(15000);
  15. conn.setRequestMethod("POST");
  16. conn.setDoInput(true);
  17. conn.setDoOutput(true);
  18.  
  19. OutputStream os = conn.getOutputStream();
  20. BufferedWriter writer = new BufferedWriter(
  21. new OutputStreamWriter(os, StandardCharsets.UTF_8));
  22. writer.write(getPostDataString(postDataParams));
  23.  
  24. writer.flush();
  25. writer.close();
  26. os.close();
  27. int responseCode = conn.getResponseCode();
  28.  
  29. if (responseCode == HttpsURLConnection.HTTP_OK) {
  30. String line;
  31. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  32. while ((line = br.readLine()) != null) {
  33. response += line;
  34. }
  35. }
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. Crashlytics.logException(e);
  39. try {
  40. response = conn.getResponseMessage();
  41. } catch (IOException e1) {
  42. e1.printStackTrace();
  43. Crashlytics.logException(e1);
  44. }
  45. } finally {
  46. if (conn != null) {
  47. conn.disconnect();
  48. }
  49. }
  50.  
  51. try {
  52. return new HttpResponse(response, conn.getResponseCode(), conn.getResponseMessage());
  53. } catch (IOException e1) {
  54. Crashlytics.logException(e1);
  55. return new HttpResponse(response, HttpResponse.UNKNOWN_ERROR, response);
  56. }
  57. }
  58.  
  59. public static SSLSocketFactory getSSLSocketFactory() {
  60. try {
  61. // Create a trust manager that does not validate certificate chains
  62. final TrustManager[] trustAllCerts = new TrustManager[]{
  63. new X509TrustManager() {
  64. @Override
  65. public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
  66. }
  67.  
  68. @Override
  69. public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
  70. }
  71.  
  72. @Override
  73. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  74. return new java.security.cert.X509Certificate[]{};
  75. }
  76. }
  77. };
  78.  
  79. // Install the all-trusting trust manager
  80. final SSLContext sslContext = SSLContext.getInstance("SSL");
  81. sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
  82. // Create an ssl socket factory with our all-trusting manager
  83. final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
  84.  
  85. return sslSocketFactory;
  86. } catch (KeyManagementException | NoSuchAlgorithmException e) {
  87. return null;
  88. }
  89.  
  90. }
  91.  
  92. private static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
  93. StringBuilder result = new StringBuilder();
  94. boolean first = true;
  95. for (Map.Entry<String, String> entry : params.entrySet()){
  96. if (first)
  97. first = false;
  98. else
  99. result.append("&");
  100.  
  101. result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  102. result.append("=");
  103. result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  104. }
  105.  
  106. return result.toString();
  107. }
  108.  
  109. private static HttpResponse performGetRequest(String urlString) {
  110. URL url;
  111. HttpsURLConnection conn = null;
  112. String response = "";
  113.  
  114. // Log.w(TAG, "performGetRequest: " + urlString);
  115.  
  116. try {
  117. url = new URL(urlString);
  118.  
  119. conn = (HttpsURLConnection) url.openConnection();
  120. //conn.setSSLSocketFactory(new TLSSocketFactory());
  121. conn.setSSLSocketFactory(getSSLSocketFactory());
  122.  
  123. int responseCode = conn.getResponseCode();
  124.  
  125. if (responseCode == HttpsURLConnection.HTTP_OK) {
  126. String line;
  127. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  128. while ((line = br.readLine()) != null) {
  129. response += line;
  130. }
  131. }
  132.  
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. } finally {
  136. if (conn != null) {
  137. conn.disconnect();
  138. }
  139. }
  140.  
  141. try {
  142. return new HttpResponse(response, conn.getResponseCode(), conn.getResponseMessage());
  143. } catch (Exception e1) {
  144. return new HttpResponse(response, HttpResponse.UNKNOWN_ERROR, response);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement