java 用面向对象的方式发送http的GET和POST请求

直接上代码把,伸手党麻烦点下赞,谢谢。

/**
 * 通用封装发送HTTP请求
 * @author dan 
 * create 2014-3-24上午10:18:55
 */

public class HttpSender<T> {
    private static Log log = LogFactory.getLog(HttpSender.class);
	/**
	 * 发送实体
	 * @param obj 发送类的实体
	 * @param url 发送的url
	 * @return
	 */
	public String sendPost(T obj, String url) {
		try {
			BeanInfo bif = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertis = bif.getPropertyDescriptors();

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (PropertyDescriptor pd : propertis) {
				Object o = pd.getReadMethod().invoke(obj, null);
				String str = null;
				if(o!=null){
				   str = o.toString();
				}
				params.add(new BasicNameValuePair(pd.getName(), str));
			}
			UrlEncodedFormEntity uefEntity;
			uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
			post.setEntity(uefEntity);
			// 设置请求表单
			HttpResponse response = httpclient.execute(post);
			HttpEntity entity = response.getEntity();
			if (entity == null) {
				return "";
			}
			return EntityUtils.toString(entity);
		} catch (Exception e) {
			e.printStackTrace();
            log.error("自省出错!"+e);
		}
		return "";
	}

    public String sendGet(T obj, String url) {
        try {
            BeanInfo bif = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertis = bif.getPropertyDescriptors();

            HttpClient httpclient = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            HttpParams httpParams = new BasicHttpParams();
            for (PropertyDescriptor pd : propertis) {
                Object o = pd.getReadMethod().invoke(obj, null);
                String str = null;
                if(o!=null){
                    str = o.toString();
                }
                httpParams.setParameter(pd.getName(), str);
            } 
            get.setParams(httpParams);
            // 设置请求表单
            HttpResponse response = httpclient.execute(get);
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                return "";
            }
            return EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("自省出错!"+e);
        }
        return "";
    }
}

以下是测试代码

@Test
    public void testGet(){
        String str = "http://192.168.1.4:8088/Charge/Charges";
        HttpSender<TransferBean> sender = new HttpSender<TransferBean>();
        TransferBean transferBean = new TransferBean();
        transferBean.setAccountID("123");
        String result = sender.sendGet(transferBean,str);
        System.out.print(result);
    }



测试过程中,还是必须要带参数的否则会报空指针,导致内省失败,有空再改。

转载于:https://my.oschina.net/u/167671/blog/228389

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值