package com.sdutacm.sportacm;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * Created by bummer on 2017/8/1.
 */

public class HttpUtil {
    static HttpURLConnection conn = null;
    static InputStreamReader input = null;
    static BufferedReader buffer = null;
    static PrintWriter print = null;
    public static void httpconnect(String url){
        try {
            URL httpUrl = new URL(url);
            conn = (HttpURLConnection) httpUrl.openConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String Get(){
        String inputLine = null;
        String result = "";
        try {
           input = new InputStreamReader(conn.getInputStream());
            buffer = new BufferedReader(input);
            while ((inputLine = buffer.readLine())!=null){
                result +=inputLine+"\n";
            }
            buffer.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return result;
    }
    public static String post(String params){
        String result = "";

        try {
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            OutputStream out = conn.getOutputStream();
            print = new PrintWriter(out);
            print.print(params);
            print.flush();
            InputStreamReader input = new InputStreamReader(conn.getInputStream());
            BufferedReader reader = new BufferedReader(input);
            String inputLine = null;
            while ((inputLine = reader.readLine())!=null){
                result += inputLine +"\n";
            }
            print.close();
            conn.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public void close(){
        if(conn != null){
            conn.disconnect();
        }
    }
    public static String clientpost(String url, List<NameValuePair> params){
        //发送POST请求,创建HttpPost对象
        HttpPost httpRequest = new HttpPost(url);
        HttpEntity httpEntity;
        String result = "";
        try {
            httpEntity = new UrlEncodedFormEntity( params,"utf-8");
            httpRequest.setEntity(httpEntity);
            /*HttpClient 是Http的一个借口,因此,HttpClient对象需要通过DefaultHttpClient来进行实例化
            * */
            HttpClient httpClient = new DefaultHttpClient();
            //发送请求,获取响应 该方法将获得Http的响应
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            //判断请求和相应是否成功
            if(httpResponse.getStatusLine().getStatusCode() ==
                    HttpStatus.SC_OK){
                //调用EntityUtils.toString()方法可获取封装在HttpEntity对象中的服务器的响应内容
                result = EntityUtils.toString(httpResponse.getEntity());
            }else {
                result = "请求错误";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  result;
    }
}