package com.cirle.scientific.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.Map;

public class HttpClientUtil {

	public static String sendGetSSL(String url) {
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpGet = new HttpGet(url);

			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public static String sendGetSSL(String url, Map<String, String> headMap) {
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpGet = new HttpGet(url);

			if (headMap != null && headMap.size() > 0) {
				for (Map.Entry<String, String> entry : headMap.entrySet()) {
					httpGet.setHeader(entry.getKey(), entry.getValue());
				}
			}
			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public static String sendPostSSL(String url, String postData, String outFile) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		InputStream in = null;
		OutputStream outputStream = null;
		try {
			httpClient = new SSLClient();
			httpPost = new HttpPost(url);

			httpPost.setHeader("Accept", "application/json");
			httpPost.setHeader("Content-Type", "application/json");
			String charSet = "UTF-8";
			StringEntity entity = new StringEntity(postData, charSet);

			httpPost.setEntity(entity);

			HttpResponse response = httpClient.execute(httpPost);

			if (response != null) {
				int code = response.getStatusLine().getStatusCode();
				if (code == 200) {
					in = response.getEntity().getContent();

					File file = new File(outFile);
					if (!file.exists()) {
						file.createNewFile();
					}
					outputStream = new FileOutputStream(file);
					int len = 0;
					byte[] buf = new byte[1024];
					while ((len = in.read(buf, 0, 1024)) != -1) {
						outputStream.write(buf, 0, len);
					}
					outputStream.flush();

					String filePath = outFile.substring(outFile.lastIndexOf("/") + 1);

					String aliyunKey = "upload/sappfiles/ymgys/images/" + filePath;
//					OSSUtils.uploadFile("mevos-web", aliyunKey, outFile, "image/jpeg");

					result = aliyunKey;
				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static void main(String[] args) {
		// System.out.println(sendGetSSL(
		// "https://api.weixin.qq.com/sns/jscode2session?appid=wxc9d073d18dae9b76&secret=b3c636572a78dc948344969a6b4971e0&js_code=0016hgya2HINCR0fc9Ba2PPRxa26hgyu&grant_type=authorization_code"));
		// String s = "{\"errcode\":40163,\"errmsg\":\"code been used, hints: [ req_id:
		// oKwIMA0999th50 ]\"}";
		// JSONObject resultJson = JSONObject.fromObject(s);
		// System.out.println("根据登录验证code获取session_key的URL请求返回结果：" + resultJson);

		String dd = "/usr/local/tomcat/tmpfies/3848dieiekek.png";
		System.out.println(dd.substring(dd.lastIndexOf("/") + 1));

	}
}
