package com.platform.http.gfs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.platform.controller.DataModelController;
import com.platform.utils.Bean2MapUtils;
import com.platform.utils.Configs;

public class HttpUtils {
	
	public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(HttpUtils.class);

	public String sendPost(String subUrl, Object data) {
		String resultStr = "";
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(HttpClientConstant.URL_IP_PORT + subUrl);
		try {
			// 传参
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			
			if (null != data) {
				// 转 map
				Map<String, Object> map = Bean2MapUtils.convertBean(data);
				// 转json
				JSONObject jsondata = JSONObject.fromObject(map);
				// 封装数据
				parameters.add(new BasicNameValuePair("jsondata", jsondata
						.toString()));
				parameters.add(new BasicNameValuePair("type", data.getClass()
						.getName()));
				// 传参 放入 url的 form中
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
						"UTF-8");
				post.setEntity(entity);
			}
			
			// 发送
			HttpResponse respone = client.execute(post);
			HttpEntity result = respone.getEntity();
			if (null != result) {
				resultStr = EntityUtils.toString(result);
			}
//			resultMap = Bean2MapUtils.convertBean(result);
		} catch (Exception e) {
			System.out.println(e);
		}
		return resultStr;
	}

//	public static void main(String[] args) {
//		Date d = new Date();
//		HttpUtils ht = new HttpUtils();
//		BaseForm1 bas = new BaseForm1();
//		bas.setId("12");
//		bas.setName("n12");
//		 String rest = ht.sendPost("gfs/getAllvolume", null);
//		if (null != rest) {
//			System.out.println(rest);
//			JSONArray jsondata = JSONArray.fromObject(rest);
//			ArrayList<VolumeEntity> fo = (ArrayList<VolumeEntity>) JSONArray.toCollection(jsondata, VolumeEntity.class);
//			System.err.println(fo);
//			try {
//				System.err.println(Bean2MapUtils.convertBean(fo));
//			} catch (IllegalAccessException | InvocationTargetException
//					| IntrospectionException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		
//		Date r = new Date();
//		System.err.println(r.getTime() - d.getTime());
//	}

}
