package com.samourai.sentinel.util;

import org.apache.commons.io.IOUtils;

import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Web	{

    public static final String SAMOURAI_API = "https://api.samouraiwallet.com/";
    public static final String SAMOURAI_API2 = "https://api.samouraiwallet.com/v2/";
    public static final String LBC_EXCHANGE_URL = "https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/";
    public static final String BTCe_EXCHANGE_URL = "https://btc-e.com/api/3/ticker/";
    public static final String BFX_EXCHANGE_URL = "https://api.bitfinex.com/v1/pubticker/btcusd";
    public static final String _21CO_FEE_URL = "https://bitcoinfees.21.co/api/v1/fees/recommended";

    private static final int DefaultRequestRetry = 2;
    private static final int DefaultRequestTimeout = 60000;

    public static String postURL(String request, String urlParameters) throws Exception {

        String error = null;

        for (int ii = 0; ii < DefaultRequestRetry; ++ii) {
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            try {
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("charset", "utf-8");
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");

                connection.setUseCaches (false);

                connection.setConnectTimeout(DefaultRequestTimeout);
                connection.setReadTimeout(DefaultRequestTimeout);

                connection.connect();

                DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                connection.setInstanceFollowRedirects(false);

                if (connection.getResponseCode() == 200) {
//					Log.d("postURL", "return code 200");
                    return IOUtils.toString(connection.getInputStream(), "UTF-8");
                }
                else {
                    error = IOUtils.toString(connection.getErrorStream(), "UTF-8");
//					Log.d("postURL", "return code " + error);
                }

                Thread.sleep(5000);
            } finally {
                connection.disconnect();
            }
        }

        throw new Exception("Invalid Response " + error);
    }

    public static String postURL(String contentType, String request, String urlParameters) throws Exception {

        String error = null;

        for (int ii = 0; ii < DefaultRequestRetry; ++ii) {
            URL url = new URL(request);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            try {
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", contentType == null ? "application/x-www-form-urlencoded" : contentType);
                connection.setRequestProperty("charset", "utf-8");
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");

                connection.setUseCaches (false);

                connection.setConnectTimeout(DefaultRequestTimeout);
                connection.setReadTimeout(DefaultRequestTimeout);

                connection.connect();

                DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                connection.setInstanceFollowRedirects(false);

                if (connection.getResponseCode() == 200) {
//					System.out.println("postURL:return code 200");
                    return IOUtils.toString(connection.getInputStream(), "UTF-8");
                }
                else {
                    error = IOUtils.toString(connection.getErrorStream(), "UTF-8");
//                    System.out.println("postURL:return code " + error);
                }

                Thread.sleep(5000);
            } finally {
                connection.disconnect();
            }
        }

        throw new Exception("Invalid Response " + error);
    }

    public static String getURL(String URL) throws Exception {

        URL url = new URL(URL);

        String error = null;

        for (int ii = 0; ii < DefaultRequestRetry; ++ii) {

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            try {
                connection.setRequestMethod("GET");
                connection.setRequestProperty("charset", "utf-8");
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");

                connection.setConnectTimeout(DefaultRequestTimeout);
                connection.setReadTimeout(DefaultRequestTimeout);

                connection.setInstanceFollowRedirects(false);

                connection.connect();

                if (connection.getResponseCode() == 200)
                    return IOUtils.toString(connection.getInputStream(), "UTF-8");
                else
                    error = IOUtils.toString(connection.getErrorStream(), "UTF-8");

                Thread.sleep(5000);
            } finally {
                connection.disconnect();
            }
        }

        return error;
    }

}

@@ -466,94 +466,8 @@
 /%22;%0A
-    public static final String BTCe_EXCHANGE_URL = %22https://btc-e.com/api/3/ticker/%22;%0A
     
@@ -2911,2229 +2911,8 @@
  %7D%0A%0A
-    public static String postURL(String contentType, String request, String urlParameters) throws Exception %7B%0A%0A        String error = null;%0A%0A        for (int ii = 0; ii %3C DefaultRequestRetry; ++ii) %7B%0A            URL url = new URL(request);%0A            HttpURLConnection connection = (HttpURLConnection) url.openConnection();%0A            try %7B%0A                connection.setDoOutput(true);%0A                connection.setDoInput(true);%0A                connection.setInstanceFollowRedirects(false);%0A                connection.setRequestMethod(%22POST%22);%0A                connection.setRequestProperty(%22Content-Type%22, contentType == null ? %22application/x-www-form-urlencoded%22 : contentType);%0A                connection.setRequestProperty(%22charset%22, %22utf-8%22);%0A                connection.setRequestProperty(%22Accept%22, %22application/json%22);%0A                connection.setRequestProperty(%22Content-Length%22, %22%22 + Integer.toString(urlParameters.getBytes().length));%0A                connection.setRequestProperty(%22User-Agent%22, %22Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36%22);%0A%0A                connection.setUseCaches (false);%0A%0A                connection.setConnectTimeout(DefaultRequestTimeout);%0A                connection.setReadTimeout(DefaultRequestTimeout);%0A%0A                connection.connect();%0A%0A                DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());%0A                wr.writeBytes(urlParameters);%0A                wr.flush();%0A                wr.close();%0A%0A                connection.setInstanceFollowRedirects(false);%0A%0A                if (connection.getResponseCode() == 200) %7B%0A//%09%09%09%09%09System.out.println(%22postURL:return code 200%22);%0A                    return IOUtils.toString(connection.getInputStream(), %22UTF-8%22);%0A                %7D%0A                else %7B%0A                    error = IOUtils.toString(connection.getErrorStream(), %22UTF-8%22);%0A//                    System.out.println(%22postURL:return code %22 + error);%0A                %7D%0A%0A                Thread.sleep(5000);%0A            %7D finally %7B%0A                connection.disconnect();%0A            %7D%0A        %7D%0A%0A        throw new Exception(%22Invalid Response %22 + error);%0A    %7D%0A
 %0A   

remove dead code