package com.android.common.http;

import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class HttpUtils {

    private static final int READ_TIME_OUT = 10000;
    private static final int CONN_TIME_OUT = 10000;

    private static final String EQUAL_SIGN = "=";
    private static final String CONN_SIGN = "&";


    public static String doHttpGet(String httpUrl){
        String result = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection)(url.openConnection());
            conn.setRequestMethod("GET");
            conn.setReadTimeout(READ_TIME_OUT);
            conn.setConnectTimeout(CONN_TIME_OUT);
            conn.setRequestProperty("accept", "*/*");
            conn.connect();

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line=reader.readLine())!=null){
                result +="\n"+line;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(reader!=null){
                    reader.close();
                }
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }

        return result;
    }

    public static String doHttpPost(String httpUrl, HashMap<String,String> params) {
        String result = "";
        DataOutputStream out = null;
        BufferedReader reader = null;
        for(int k = 0; k< 3;k++){
            try {
                URL url = new URL(httpUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setReadTimeout(READ_TIME_OUT);
                connection.setConnectTimeout(CONN_TIME_OUT);
                connection.connect();

                if(params!=null){
                    out = new DataOutputStream(connection.getOutputStream());
                    String paramContent = "";
                    Set<String> keySet = params.keySet();
                    Iterator<String> iterator = keySet.iterator();
                    while (iterator.hasNext()) {
                        String paramName = iterator.next();
                        String paramValue = params.get(paramName);
                        if (!TextUtils.isEmpty(paramContent)) {
                            paramContent += CONN_SIGN;
                        }
                        paramContent += paramName + EQUAL_SIGN + URLEncoder.encode(paramValue, "UTF-8");
                    }
                    out.write(paramContent.getBytes());
                    out.flush();
                }
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    result +=  line;
                }
                return result;
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    if(out!=null){
                        out.close();
                    }
                    if(reader!=null){
                        reader.close();
                    }
                }catch (IOException ex){
                    ex.printStackTrace();
                }
            }
        }
        return result;
    }


}
