package webapi.demo;

import org.apache.commons.io.Charsets;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

import webapi.demo.pojo.CloudDbRequest;

public class CloubDbDetail {
	/**
	 * 接口地址
	 */
	private String url = "https://cloud.voidar.net/develop/ws/cloudDbDetail.do";

	private String getRequestBody() {
		try {
			String account = "test@voidar.net";
			String password = "123456";
			String name = "my_cloud_database";
			CloudDbRequest requestBody = new CloudDbRequest(account, password, name);
			Gson gson = new Gson();
			return gson.toJson(requestBody);
		} catch (Exception e) {
			return null;
		}
	}

	private void postData() {
		CloseableHttpClient httpclient = HttpUtils.createSSLInsecureClient();
		CloseableHttpResponse response = null;
		try {
			String requestBody = this.getRequestBody();
			System.out.println(requestBody);
			if (StringUtils.isBlank(requestBody)) {
				System.out.println("请求数据错误");
				System.exit(1);
			}
			StringEntity entity = new StringEntity(requestBody, Charsets.UTF_8);
			HttpPost post = new HttpPost(this.url);
			post.setHeader("Content-Type", "application/json");
			post.setEntity(entity);
			response = httpclient.execute(post);
			String resp = EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
			System.out.println(resp);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (Exception e) {
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e) {
				}
			}
		}
	}

	public static void main(String[] args) {
		CloubDbDetail task = new CloubDbDetail();
		task.postData();
	}
}
