Apach HttpClient

package com.jetsum.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
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.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;



public class HttpClientUtil {

	private static String JETSUM_PLATFORM_SERVER = null;
	public static String JETSUM_PLATFORM_BEANMONITOR = "0";// 0:不监听service
	public static String JETSUM_PLATFORM_REQUESTMONITOR = "0";// 0:不监听request

	static {
		try {
			PropertiesUtil.load("/remoteServices.properties");
			JETSUM_PLATFORM_SERVER = PropertiesUtil.get("remoteServerAddress");
		} catch (Exception e) {
			System.out.println("读取平台配置文件出错! 异常是:"+e);
		}
	}
	
    /**
     * 远程调用服务POST方法
     * @param url 服务地址
     * @param map 传递参数
     * @return 服务器返回点JSON字符串
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String remotePost(String url, Map<String, String> map) throws ClientProtocolException, IOException {
    	url = JETSUM_PLATFORM_SERVER+url;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> formparams = setHttpParams(map);
        UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(param);
        HttpResponse response = httpclient.execute(httppost);
        String httpEntityContent = GetHttpEntityContent(response);
        httppost.abort();
        return httpEntityContent;
    }
    
    /**
     * HTTP DELETE方法进行删除操作
     * @param url
     * @param map
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String remoteDelete(String url, Map<String, String> map) throws ClientProtocolException, IOException{
    	url = JETSUM_PLATFORM_SERVER+url;
        HttpClient httpclient = new DefaultHttpClient();
        HttpDelete httpdelete= new HttpDelete();
        List<NameValuePair> formparams = setHttpParams(map);
        String param = URLEncodedUtils.format(formparams, "UTF-8");
        httpdelete.setURI(URI.create(url + "?" + param));
        HttpResponse response = httpclient.execute(httpdelete);
        String httpEntityContent = GetHttpEntityContent(response);
        httpdelete.abort();
        return httpEntityContent;	
    }    
    
    /**
     * HTTP PUT方法进行修改操作
     * @param url
     * @param map
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String remotePut(String url, Map<String, String> map) throws ClientProtocolException, IOException{
       	url = JETSUM_PLATFORM_SERVER+url;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPut httpput = new HttpPut(url);
        List<NameValuePair> formparams = setHttpParams(map);
        UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
        httpput.setEntity(param);
        HttpResponse response = httpclient.execute(httpput);
        String httpEntityContent = GetHttpEntityContent(response);
        httpput.abort();
        return httpEntityContent;   	
    }   
    
    /**
     * 远程调用服务GET方法
     * @param url 服务地址
     * @param map 传递参数,参数中如有中文需要客户端转码
     * @return 服务器返回点JSON字符串
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String remoteGet(String url, Map<String, String> map) throws ClientProtocolException, IOException {
    	url = JETSUM_PLATFORM_SERVER+url;
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet();
        List<NameValuePair> formparams = setHttpParams(map);
        String param = URLEncodedUtils.format(formparams, "UTF-8");
        httpget.setURI(URI.create(url + "?" + param));
        HttpResponse response = httpclient.execute(httpget);
        String httpEntityContent = GetHttpEntityContent(response);
        httpget.abort();
        return httpEntityContent;
    }
    
    /**
     * 设置请求参数
     * @param map
     * @return
     */
	private static List<NameValuePair> setHttpParams(Map<String, String> map) {
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> entry : set) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
		return formparams;
	}

	/**
	 * 获得响应HTTP实体内容
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	private static String GetHttpEntityContent(HttpResponse response)
			throws IOException, UnsupportedEncodingException {
		HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = br.readLine();
            StringBuilder sb = new StringBuilder();
            while (line != null) {
                sb.append(line + "\n");
                line = br.readLine();
            }
            return sb.toString();
        }
        return "";
	}
}

 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sjiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值