package core.stlmes.server.sale.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConnectionLL {
	public static String postJsonParams(String requestUrl,   String params) {
		StringBuilder sb = new StringBuilder();
		try {
			URL url = new URL(requestUrl);// 创建连接
			HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestMethod("POST"); // 设置请求方式
			connection.setConnectTimeout(5000); // 规定20秒内必须响应
			connection.setReadTimeout(5000); // 设置读取超时为20秒
			connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
			connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
			connection.connect();
			OutputStreamWriter out = new OutputStreamWriter(
				connection.getOutputStream(), "UTF-8"); // utf-8编码
			out.append(params);
			out.flush();
			out.close();
			if (connection != null) {
				InputStream inputStream = null;
				//根据responseCode来获取输入流，此处错误响应码的响应体内容也要获取（看服务端的返回结果形式决定）
				if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
					inputStream = connection.getInputStream();
				} else {
					inputStream = connection.getErrorStream();
				}
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line);
					sb.append("\r\n");
				}
				br.close();
			}
			connection.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	// delete:
	public static String deleteJsonParams(String requestUrl) {
		StringBuilder sb = new StringBuilder();
		try {
			URL url = new URL(requestUrl);// 创建连接
			HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestMethod("DELETE"); // 设置请求方式
			connection.setConnectTimeout(5000); // 规定20秒内必须响应
			connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
			connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
			connection.connect();
			OutputStreamWriter out = new OutputStreamWriter(
				connection.getOutputStream(), "UTF-8"); // utf-8编码
//			out.append(params);
			out.flush();
			out.close();
			if (connection != null) {
				InputStream inputStream = null;
				//根据responseCode来获取输入流，此处错误响应码的响应体内容也要获取（看服务端的返回结果形式决定）
				if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
					inputStream = connection.getInputStream();
				} else {
					inputStream = connection.getErrorStream();
				}
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line);
					sb.append("\r\n");
				}
				br.close();
			}
			connection.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}
