package com.platform.test;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;






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.entity.StringEntity;
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 org.springframework.http.HttpStatus;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.entities.CheckoutEntity;
import com.platform.form.PagerOptions;
import com.platform.http.HttpClientConstant;
import com.platform.http.HttpUtils;

public class httpclientTest {
	
	private static ObjectMapper mapper = new ObjectMapper();
	
	public final static Logger log = Logger.getLogger(HttpUtils.class);
	
	/** 发post请求
	 * @param subUrl
	 * @param map
	 * @return
	 */
	public String sendAjaxPost(String subUrl, String json) {
		String resultStr = "";
		HttpClient client = new DefaultHttpClient();
		
		HttpPost post = new HttpPost(subUrl);
		try {
			// 传参
			if (null != json && !"".equals(json)) {
				 // 建立一个NameValuePair数组，用于存储欲传送的参数  
				StringEntity entity = new StringEntity(json,"UTF-8");
				entity.setContentEncoding("UTF-8");    
                entity.setContentType("application/json");  
                
				post.setEntity(entity);
			}
			// 发送
			HttpResponse respone = client.execute(post);
			//接收返回值
			if(respone.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
				HttpEntity result = respone.getEntity();
				if (null != result) {
					resultStr = EntityUtils.toString(result);
				}
			}
		} catch (Exception e) {
			log.info(HttpClientConstant.URL_IP_PORT + subUrl);
			log.error(e);
		}
		return resultStr;
	}
	
	/** post请求前 对象 转成 json
	 * @param subUrl
	 * @param data
	 * @return
	 */
	public String sendAjaxPost(String subUrl, Object data) {
		String jsondata = null;
		//转成json
		if (null != data) {
			try {
				if (data.getClass().getName().contains("java.util")) {
					Writer strWriter = new StringWriter();
					mapper.writeValue(strWriter, data);
					jsondata = strWriter.toString();
				}
				else {
					Writer strWriter = new StringWriter();
					mapper.writeValue(strWriter, data);
					jsondata = strWriter.toString();
				}
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sendAjaxPost(subUrl, jsondata);
	}
	
	public static void main(String[] args) {
		httpclientTest ht = new httpclientTest();
		PagerOptions p = new PagerOptions();
		p.setCurrentPageNum(1);
		p.setLimit(20);
		p.setVolumeType("1");
		System.out.println(ht.sendAjaxPost("http://127.0.0.1:8080/checkout/checkList", p));
		List<CheckoutEntity> lis = new ArrayList<CheckoutEntity>();
		CheckoutEntity ck = new CheckoutEntity();
		ck.setId(2);	
		ck.setAreaCode("320000");
		lis.add(ck);
		System.out.println(ht.sendAjaxPost("http://127.0.0.1:8080/checkout/checkList", lis));
		
	}
}
