package com.chinacreator.process.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;

public class URLUtil {

	
	/**
	 * 解读流
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static String readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = null;
		try {
			outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			byte[] data = outStream.toByteArray();
			return new String(data, "utf-8");
		} finally{
			if(outStream != null)
				outStream.close();
			if(inStream != null)
				inStream.close();
		}
	}
	
	/**
	 * 以get方式获取链接的输出
	 * @param href
	 * @return
	 * @throws Exception
	 */
	public static String get(String href, int timeout) throws Exception {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(href);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			if(timeout > 0) {
				conn.setConnectTimeout(timeout);
				conn.setReadTimeout(timeout);
			}
			conn.addRequestProperty("content-type", "text/plain");
			conn.setUseCaches(false);
			conn.setDoOutput(false);
			return readInputStream(conn.getInputStream());
		} finally {
			if(conn != null) {
				conn.disconnect();
			}
		}
	}
	
	public static String get(String href) throws Exception {
		return get(href, 0);
	}

	public static String get(String href, HashMap<String,String> map) throws Exception {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(href);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.addRequestProperty("content-type", "text/plain");
			if (map!=null&&map.size()>0){
				Set<String> strings = map.keySet();
				for (String key:strings){
					conn.addRequestProperty(key,map.get(key));
				}
			}
			conn.setUseCaches(false);
			conn.setDoOutput(false);
			return readInputStream(conn.getInputStream());
		} finally {
			if(conn != null) {
				conn.disconnect();
			}
		}
	}
	/**
	 * 以post方式获取链接的输出
	 * @param href
	 * @return
	 * @throws Exception
	 */
	public static String post(String href, String data, int timeout, String contentType) throws Exception {
		HttpURLConnection conn = null;
		OutputStreamWriter out = null;
		if(data == null || data.length() == 0) {
			return get(href, timeout);
		} else {
			try {
				URL url = new URL(href);
				if ("https".equalsIgnoreCase(url.getProtocol())) {
					SslUtils.ignoreSsl();
				}
				conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("POST");
				if(timeout > 0) {
					conn.setConnectTimeout(timeout);
					conn.setReadTimeout(timeout);
				}
				conn.addRequestProperty("content-type", contentType);
				conn.setDoOutput(true);
				conn.setUseCaches(false);
				out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
				out.write(data);
				out.flush();
			} finally {
				if(out != null) {
					out.close();
				}
				if(conn != null) {
					conn.disconnect();
				}
			}
			if(conn != null) {
				return readInputStream(conn.getInputStream());
			}
			return null;
		}
	}
	
	public static String postJson(String href, String data) throws Exception {
		return post(href, data, 0, "application/json;charset=utf-8");
	}
	
	public static String postJson(String href, String data, int timeout) throws Exception {
		return post(href, data, timeout, "application/json;charset=utf-8");
	}
	
	public static String post(String href, String data, int timeout) throws Exception {
		return post(href, data, timeout, "text/plain");
	}
	
	public static String post(String href, String data) throws Exception {
		return post(href, data, 0);
	}
	
	public static String postForm(String href, String data, int timeout) throws Exception {
		return post(href, data, timeout, "application/x-www-form-urlencoded");
	}
	
	public static String postForm(String href, String data) throws Exception {
		return postForm(href, data, 0);
	}
	
	public static void main(String[] args) throws Exception {
		String url = "http://114.255.201.228:86/mobile-report/sendAlarmMsg.do";
		String result = postForm(url, "name=CDN_BUSINESS&content=测试短信");
		System.out.println(result);
	}
}
