// Path: app/src/main/java/org/pquery/webdriver/CancelledListener.java
// public interface CancelledListener {
//     void ifCancelledThrow() throws InterruptedException;
// }
// 
// Path: app/src/main/java/org/pquery/webdriver/HttpClientFactory.java
// public class HttpClientFactory {
// 
// //    public static DefaultHttpClient createHttpClient() {
// //
// //        // Set timeout
// //        // I have seen some random failures where the network access just seems to lock-up for over a minute
// //        // I can't reproduce it but hopefully this will make it error and allow user to manually retry
// //        final HttpParams httpParams = new BasicHttpParams();
// //        HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
// //        HttpConnectionParams.setSoTimeout(httpParams, 10000);
// //
// //        return new DefaultHttpClient(httpParams);
// //    }
// 
//     public static HttpURLConnection createURLConnection(URL url) throws IOException {
//         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 
//         // Set timeout
//         // I have seen some random failures where the network access just seems to lock-up
//         // ... for minutes
//         // I can't reproduce it but hopefully this will make it error out and allow user to manually
//         // retry
//         conn.setReadTimeout(20000);         // 20 seconds
//         conn.setConnectTimeout(20000);      // 20 seconds
// 
//         conn.setRequestProperty("Accept-Encoding", "gzip");
//         conn.setRequestProperty("Host", "www.geocaching.com");
// 
//         // Makes our job a bit easier trying to work out what is going on with login problems etc.
//         conn.setInstanceFollowRedirects(false);
// 
//         return conn;
//     }
// }


import android.content.Context;
import android.util.Pair;
import org.pquery.webdriver.CancelledListener;
import org.pquery.webdriver.HttpClientFactory;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

        Map<String,String> newcookies = extractCookies(client);
        cookies.putAll(newcookies);

        Prefs.saveCookies(cxt, cookies);

        // Check for 404 page not found, 302 object moved etc
        // We read in body, even though error, because need to see the redirect link etc
        // Can assume error page is html (rather than binary) so ok to log it etc
        if (statusCode >= 300) {
            String s = new String(data);
            Logger.d("Bad status page response body.. " + s);
            throw new HTTPStatusCodeException(client.getResponseCode(), client.getResponseMessage(), s);
        }

        Logger.d("returning " + data.length + " bytes");

        FileDetails ret = new FileDetails();
        ret.filename = filename;
        ret.contents = data;

        return ret;
    }

    public static String httpPost(Context cxt, List<Pair<String,String>> paramList, String path, boolean secure, CancelledListener cancelledListener, Listener listener) throws IOException, InterruptedException {

        Logger.d("enter [path=" + getSecureHost() + path + "]");

        URL url = new URL(getSecureHost() + path);

// Path: app/src/main/java/org/pquery/webdriver/CancelledListener.java
// public interface CancelledListener {
//     void ifCancelledThrow() throws InterruptedException;
// }
// 
// Path: app/src/main/java/org/pquery/webdriver/HttpClientFactory.java
// public class HttpClientFactory {
// 
// //    public static DefaultHttpClient createHttpClient() {
// //
// //        // Set timeout
// //        // I have seen some random failures where the network access just seems to lock-up for over a minute
// //        // I can't reproduce it but hopefully this will make it error and allow user to manually retry
// //        final HttpParams httpParams = new BasicHttpParams();
// //        HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
// //        HttpConnectionParams.setSoTimeout(httpParams, 10000);
// //
// //        return new DefaultHttpClient(httpParams);
// //    }
// 
//     public static HttpURLConnection createURLConnection(URL url) throws IOException {
//         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 
//         // Set timeout
//         // I have seen some random failures where the network access just seems to lock-up
//         // ... for minutes
//         // I can't reproduce it but hopefully this will make it error out and allow user to manually
//         // retry
//         conn.setReadTimeout(20000);         // 20 seconds
//         conn.setConnectTimeout(20000);      // 20 seconds
// 
//         conn.setRequestProperty("Accept-Encoding", "gzip");
//         conn.setRequestProperty("Host", "www.geocaching.com");
// 
//         // Makes our job a bit easier trying to work out what is going on with login problems etc.
//         conn.setInstanceFollowRedirects(false);
// 
//         return conn;
//     }
// }


// Path: app/src/main/java/org/pquery/util/IOUtils.java
import android.content.Context;
import android.util.Pair;
import org.pquery.webdriver.CancelledListener;
import org.pquery.webdriver.HttpClientFactory;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;


        Map<String,String> newcookies = extractCookies(client);
        cookies.putAll(newcookies);

        Prefs.saveCookies(cxt, cookies);

        // Check for 404 page not found, 302 object moved etc
        // We read in body, even though error, because need to see the redirect link etc
        // Can assume error page is html (rather than binary) so ok to log it etc
        if (statusCode >= 300) {
            String s = new String(data);
            Logger.d("Bad status page response body.. " + s);
            throw new HTTPStatusCodeException(client.getResponseCode(), client.getResponseMessage(), s);
        }

        Logger.d("returning " + data.length + " bytes");

        FileDetails ret = new FileDetails();
        ret.filename = filename;
        ret.contents = data;

        return ret;
    }

    public static String httpPost(Context cxt, List<Pair<String,String>> paramList, String path, boolean secure, CancelledListener cancelledListener, Listener listener) throws IOException, InterruptedException {

        Logger.d("enter [path=" + getSecureHost() + path + "]");

        URL url = new URL(getSecureHost() + path);

        HttpURLConnection client = HttpClientFactory.createURLConnection(url);