package com.connor.erp.toolkit;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	public static String doGet(String url) throws Exception {

		// url 路径
		String jString="";
		HttpClient client = new DefaultHttpClient();

		client.getParams().setParameter("http.connection.timeout", Integer.valueOf(10000));
		client.getParams().setParameter("http.socket.timeout", Integer.valueOf(20000));
		HttpGet getmeth = new HttpGet(url);
		try {

			HttpResponse statueCode = client.execute(getmeth);

			System.out.println(statueCode.getStatusLine());// 状态码 200成功 500服务器内部报错这个时候要看服务端的报错日志 404 路径错误

			String response = paseResponse(statueCode);
			
			return response;
		} catch (ConnectTimeoutException cte) {
			cte.printStackTrace();
		      System.out.println("请求通信【"+ url + "】时连接超时，堆栈轨迹如下"+ cte.getStackTrace());
	    } catch (SocketTimeoutException ste) {
	    	ste.printStackTrace();
	      System.out.println("请求通信【"+ url + "】时读取超时，堆栈轨迹如下"+ ste.getStackTrace());
	    } catch (Exception e) {
	    	e.printStackTrace();
	      System.out.println("请求通信【"+ url + "】时偶遇异常，堆栈轨迹如下"+ e.getStackTrace());
	    } finally {
			client.getConnectionManager().shutdown();
		}
		return jString;
	}

	public static String doPost(String url, String param) throws Exception {

		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter("http.connection.timeout", Integer.valueOf(10000));
		client.getParams().setParameter("http.socket.timeout", Integer.valueOf(20000));
		HttpPost postmeth = new HttpPost(url);
		StringEntity stringEntity = new StringEntity(param, "UTF-8");
//		stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
//                "application/json"));
		postmeth.setEntity(stringEntity); // post请求参数 param可以在请求之前进行json转换
//		postmeth.setHeader("Content-Type", "application/json;charset=utf-8");
		postmeth.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		try {

			HttpResponse statueCode = client.execute(postmeth);

			System.out.println(statueCode.getStatusLine());// 状态码 200成功 500服务器内部报错这个时候要看服务端的报错日志 404 路径错误

			String response = paseResponse(statueCode);
			
			return response;
		} catch (ConnectTimeoutException cte) {
		      System.out.println("请求通信【"+ url + "】时连接超时，堆栈轨迹如下"+ cte.getStackTrace());
	    } catch (SocketTimeoutException ste) {
	      System.out.println("请求通信【"+ url + "】时读取超时，堆栈轨迹如下"+ ste.getStackTrace());
	    } catch (Exception e) {
	      System.out.println("请求通信【"+ url + "】时偶遇异常，堆栈轨迹如下"+ e.getStackTrace());
	    } finally {
			client.getConnectionManager().shutdown();
		}
		return null;
	}

    private static String paseResponse(HttpResponse response) {

        HttpEntity entity = response.getEntity();

        String charset = EntityUtils.getContentCharSet(entity);

        String body = null;
        try {
            body = EntityUtils.toString(entity);

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return body;
    }
}
