package org.wuxinshui.boosters.http;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.http.HttpEntity;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import static org.wuxinshui.boosters.json.JSONUtils.JSONFormatter;

/**
 * @Description:
 * @Author:Wuxinshui
 * @Date:2017/4/27 11:23
 */
public class HttpUtils {

	public static String responseJSON(String url, int method, String jsonData) {
		return JSONFormatter(executeCall(url, method, jsonData));
	}

	public static String executeCall(String url, int method, String jsonData) {
		String response = "";
		try {
			// http client
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpEntity httpEntity = null;
			HttpResponse httpResponse = null;
			// Checking http request method type
			if (method == 0) {
				HttpPost httpPost = new HttpPost(url);
				// adding post params
				if (jsonData != null && jsonData.length() > 0) {
					StringEntity encodedEntity = new StringEntity(jsonData, "UTF-8");
					encodedEntity.setContentEncoding("UTF-8");
					encodedEntity.setContentType("application/json");
					httpPost.setEntity(encodedEntity);
				}
				httpResponse = httpClient.execute(httpPost);

			} else if (method == 1) {
				HttpGet httpGet = new HttpGet(url);
				httpResponse = httpClient.execute(httpGet);
			}
			//codigo pruebna
			response = new Scanner(httpResponse.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next();
		/*httpEntity = httpResponse.getEntity();
		response = EntityUtils.toString(httpEntity,"utf-8");*/
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static void sendPost(String url, String jsonData) {
		try {
			//创建连接
			URL connUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) connUrl
					.openConnection();
			// 发送POST请求必须设置如下两行
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/json");

			connection.connect();

			//POST请求
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			out.writeBytes(jsonData);
			out.flush();
			out.close();

			//读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			System.out.println(sb);
			reader.close();
			// 断开连接
			connection.disconnect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

@@ -2135,16 +2135,41 @@
 Data) %7B%0A
+%09%09InputStream is = null;%0A
 %09%09try %7B%0A
@@ -2783,101 +2783,305 @@
 %0A%09%09%09
-BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())
+HttpURLConnection urlConnection = connection;%0A%09%09%09if (urlConnection.getResponseCode() %3E 400) %7B%0A%09%09%09%09is = urlConnection.getErrorStream();%0A%09%09%09%7D else %7B%0A%09%09%09%09is = urlConnection.getInputStream();%0A%09%09%09%7D%0A%0A%09%09%09InputStreamReader isr = new InputStreamReader(is);%0A%09%09%09BufferedReader reader = new BufferedReader(isr
 );%0A%09
@@ -3393,46 +3393,8 @@
 ) %7B%0A
-%09%09%09// TODO Auto-generated catch block%0A
 %09%09%09e
@@ -3462,46 +3462,8 @@
 ) %7B%0A
-%09%09%09// TODO Auto-generated catch block%0A
 %09%09%09e
@@ -3485,34 +3485,32 @@
 e();%0A%09%09%7D catch (
-IO
 Exception e) %7B%0A%09
@@ -3515,47 +3515,91 @@
 %0A%09%09%09
-// TODO Auto-generated catch block%0A%09%09%09e
+if (null != is) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09is.close();%0A%09%09%09%09%7D catch (IOException e1) %7B%0A%09%09%09%09%09e1
 .pri
@@ -3610,22 +3610,33 @@
 ackTrace();%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%7D%0A%7D%0A

fix error