HttpClient使用SESSIONID模拟用户发送请求


使用HttpClient版本 4.2.5

准备一已登录用户的sessionid:

	public static void main(String[] args) {
		
		String url = "http://10.10.10.90/urcweb/api/http.do?method=get_im_info";
		
		Map<String,String> header = new HashMap<String,String>();
		header.put("Cookie", "JSESSIONID=C1A34A2EC4C9423BB460E6F7005CA81E");
		System.out.println(httpGet(url,null,header));
	}

	/**
	 * 发送 get 请求
	 * @param url
	 * @param encode
	 * @param headers
	 * @return
	 */
	public static String httpGet(String url,String encode,Map<String,String> headers){
		if(encode == null){
			encode = "utf-8";
		}
		String content = null;
		DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
       
        //设置 header
        Header headerss[] = buildHeader(headers);
        if(headerss != null && headerss.length > 0){
        	httpGet.setHeaders(headerss);
        }
        HttpResponse http_response;
		try {
			http_response = httpclient.execute(httpGet);
			HttpEntity entity = http_response.getEntity();
			content = EntityUtils.toString(entity, encode);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
            httpGet.releaseConnection();
        }
        return content;
	}
	
	/**
	 * 组装请求头
	 * @param params
	 * @return
	 */
    public static Header[] buildHeader(Map<String,String> params){
    	Header[] headers = null;
    	if(params != null && params.size() > 0){
    		headers = new BasicHeader[params.size()];
    		int i  = 0;
    		for (Map.Entry<String, String> entry:params.entrySet()) {
    			headers[i] = new BasicHeader(entry.getKey(),entry.getValue());
    			i++;
    		}
    	}
    	return headers;
    }

 转 http://huangqiqing123.iteye.com/blog/2054490

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值