Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static HttpResponse performPostRequest(String requestURL, HashMap<String, String> postDataParams) {
- URL url;
- String response = "";
- HttpsURLConnection conn = null;
- try {
- // Log.w(TAG, "performPostRequest: " + requestURL);
- url = new URL(requestURL);
- conn = (HttpsURLConnection) url.openConnection();
- //conn.setSSLSocketFactory(new TLSSocketFactory());
- conn.setSSLSocketFactory(getSSLSocketFactory());
- conn.setReadTimeout(15000);
- conn.setConnectTimeout(15000);
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, StandardCharsets.UTF_8));
- writer.write(getPostDataString(postDataParams));
- writer.flush();
- writer.close();
- os.close();
- int responseCode = conn.getResponseCode();
- if (responseCode == HttpsURLConnection.HTTP_OK) {
- String line;
- BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- while ((line = br.readLine()) != null) {
- response += line;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- Crashlytics.logException(e);
- try {
- response = conn.getResponseMessage();
- } catch (IOException e1) {
- e1.printStackTrace();
- Crashlytics.logException(e1);
- }
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- }
- try {
- return new HttpResponse(response, conn.getResponseCode(), conn.getResponseMessage());
- } catch (IOException e1) {
- Crashlytics.logException(e1);
- return new HttpResponse(response, HttpResponse.UNKNOWN_ERROR, response);
- }
- }
- public static SSLSocketFactory getSSLSocketFactory() {
- try {
- // Create a trust manager that does not validate certificate chains
- final TrustManager[] trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
- @Override
- public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
- }
- @Override
- public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
- }
- @Override
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return new java.security.cert.X509Certificate[]{};
- }
- }
- };
- // Install the all-trusting trust manager
- final SSLContext sslContext = SSLContext.getInstance("SSL");
- sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
- // Create an ssl socket factory with our all-trusting manager
- final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
- return sslSocketFactory;
- } catch (KeyManagementException | NoSuchAlgorithmException e) {
- return null;
- }
- }
- private static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
- StringBuilder result = new StringBuilder();
- boolean first = true;
- for (Map.Entry<String, String> entry : params.entrySet()){
- if (first)
- first = false;
- else
- result.append("&");
- result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
- }
- return result.toString();
- }
- private static HttpResponse performGetRequest(String urlString) {
- URL url;
- HttpsURLConnection conn = null;
- String response = "";
- // Log.w(TAG, "performGetRequest: " + urlString);
- try {
- url = new URL(urlString);
- conn = (HttpsURLConnection) url.openConnection();
- //conn.setSSLSocketFactory(new TLSSocketFactory());
- conn.setSSLSocketFactory(getSSLSocketFactory());
- int responseCode = conn.getResponseCode();
- if (responseCode == HttpsURLConnection.HTTP_OK) {
- String line;
- BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- while ((line = br.readLine()) != null) {
- response += line;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- }
- try {
- return new HttpResponse(response, conn.getResponseCode(), conn.getResponseMessage());
- } catch (Exception e1) {
- return new HttpResponse(response, HttpResponse.UNKNOWN_ERROR, response);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement