/**
 * 
 */
package com.friday.custody.service.trustService.swhy.common;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;


public class HttpClientCall {
 	public String call(Param param, String url, String token) {
		long t1 = System.currentTimeMillis();
		 
		String info="";
		
		// 创建默认的httpClient实例.    
        CloseableHttpClient httpclient = HttpClients.createDefault();  
	   
		// 创建httppost    
        HttpPost httppost = new HttpPost(url);
        
        httppost.setHeader("token", token);
        
 		
     // 创建参数队列    
        List<NameValuePair> formparams = new ArrayList<NameValuePair>(); 
		
        if(param.getBizParams()!=null && !param.getBizParams().isEmpty()){
			for (Entry<String, String> entry : param.getBizParams().entrySet()) {
				formparams.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
			}
        }
		
		UrlEncodedFormEntity uefEntity;
		try {  
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
            httppost.setEntity(uefEntity);  
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try {  
                HttpEntity entity = response.getEntity();  
                if (entity != null) {  
                    info = EntityUtils.toString(entity, "UTF-8");
                }  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
        	e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();
        } catch (IOException e2) {  
        	e2.printStackTrace();
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
                long t2 = System.currentTimeMillis();
        	 
            } catch (IOException e) {  
            }  
        }  
		return info;
	}

 	/*public String callJson(String jsonStr, String serverUrl,String token) throws Exception {
 	    CloseableHttpClient httpclient = HttpClients.createDefault();

 	    CloseableHttpResponse httpresponse=null;

 	    String httpresponseText =null;

 	    HttpPost httpPost = new HttpPost(serverUrl);
 	    
 	    httpPost.setHeader("token", token);
 	   
 	    httpPost.setEntity(new StringEntity(jsonStr, "UTF-8"));
 	    
 	    httpresponse = httpclient.execute(httpPost, new HttpClientContext());
 	    
 	    HttpEntity httpEntity = httpresponse.getEntity();

 	    httpresponseText = EntityUtils.toString(httpEntity, "UTF-8");
 	    
 	    return httpresponseText;
 		 
 	}*/


}
