HttpClientUtil

package sdcncsi.ict.site.web.xzsp;

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

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;

public class HttpClientUtil {
	/**
	 * post方式提交Http请求
	 * @param url 请求地址格式"http://localhost:9000/jnbus/servlet/HttpClientServlet";
	 * @param paramsMap
	 * @return
	 * @throws Exception
	 * @author fanglm created on May 11, 2011 9:16:06 AM
	 */
	public static String postMethod(String url,Map<String,String> paramsMap) throws Exception{
		String result = "";
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		//创建参数队列
		List<NameValuePair> formparams = getParamsList(paramsMap);
		if(formparams!=null){
			UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
			post.setEntity(uefEntity);
		}
		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();
		if(entity!=null){
			result = EntityUtils.toString(entity,"GB2312");
		}
		//System.out.println("审批大厅请求电子政务平台URL-------------------------"+url);
		//System.out.println("审批大厅请求电子政务平台参数-------------------------"+paramsMap);
		//System.out.println("审批大厅取电子政务平台返回值-------------------------"+result);
		return result;
	}
	
	public static List<NameValuePair> getParamsList(Map paramsMap){
		if(paramsMap==null||paramsMap.size()==0){
			return null;
		}
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		Set<Map.Entry> set = paramsMap.entrySet();
		for(Map.Entry<String,String> entry : set){
			list.add(new  BasicNameValuePair(entry.getKey(),entry.getValue()));
		}
		return list;
	}
}

你可能感兴趣的:(exception,String,null,url,电子政务,平台)