package com.jojubanking.boot.module.system.util.xytj;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("deprecation")
public class HttpClientUtil {

    private static final String CONTENT_TYPE_TEXT_JSON = "text/json";

    private static final RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(10000).build();
	
	@SuppressWarnings("resource")
//	public static String doPost(String url, Map<String,String> map){
    public static String doPost(String url,  Object param){
    	String charset = "utf-8";
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{  
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

            httpPost.setConfig(requestConfig);
            String parameter = JSON.toJSONString(param);

            StringEntity se = null;
            System.out.println(parameter);
            se = new StringEntity(parameter, "utf-8");
            se.setContentType(CONTENT_TYPE_TEXT_JSON);
            se.setContentEncoding("utf-8");
            httpPost.setEntity(se);

            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){  
                HttpEntity resEntity = response.getEntity();  
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);  
                }
            }  
        }catch(Exception ex){
            ex.printStackTrace();  
        }finally{
        	httpClient.getConnectionManager().shutdown();
        }
        return result;
    }  
	
	@SuppressWarnings("resource")
	public static String doGet(String url, Map<String,String> map){
    	String charset = "utf-8";
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        String result = null;
        try{  
            httpClient = new DefaultHttpClient();
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            if (map!=null) {				
            	Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
            	while(iterator.hasNext()){
            		Entry<String,String> elem = iterator.next();
            		list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));  
            	}
			}
            if(list.size() > 0){
            	String paramString = URLEncodedUtils.format(list, HTTP.UTF_8);
            	url = url +"?"+ paramString;
            }
            httpPost = new HttpGet(url);  
            HttpResponse response = httpClient.execute(httpPost);  
            if(response != null){  
                HttpEntity resEntity = response.getEntity();  
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);  
                }
            }  
        }catch(Exception ex){
            ex.printStackTrace();  
        }  
        return result;  
    }  
}  
