HttpClient使用SESSIONID模拟用户发送请求
来源:互联网 发布:iso9126软件质量模型 编辑:程序博客网 时间:2024/06/05 17:59
使用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;
- }
测试结果:
成功模拟了该登录用户,如:输出信息如下:
- {"friends":[{"id":7,"gname":"我的好友","type":null},{"id":0,"gname":"未分组","gmember":[]}],"extinfo":null,"userinfo":{"uid":"PTADMIN","icon":null,"skill":"1","user_type":"9510","uname":"平台运营者","userId":"PTADMIN"}}
阅读全文
1 0
- HttpClient使用SESSIONID模拟用户发送请求
- HttpClient使用SESSIONID模拟用户发送请求
- HttpClient使用SESSIONID模拟用户发送请求
- HttpClient使用SESSIONID模拟用户发送请求
- Httpclient(模拟用户发送请求)
- HttpClient模拟发送http请求
- java使用HttpClient模拟发送http的get、post请求
- 使用HttpClient发送请求参数
- Java使用HttpClient发送请求
- 使用HttpClient发送GET请求
- 使用HttpClient发送POST请求
- 使用httpclient发送get请求
- 使用httpclient发送post请求
- commons-httpclient 服务端模拟客户端发送请求
- HttpClient服务器模拟浏览器发送请求
- android使用apache httpclient发送post请求
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- React-native基础介绍
- Xor Sum HDU
- iOS方法--------单例模式的写法
- 同步更新测试
- POJ 2524-Ubiquitous Religions
- HttpClient使用SESSIONID模拟用户发送请求
- 常用排序算法的js实现(冒泡、选择、插入、归并、希尔、快排)
- SVN二次开发收集01
- 工厂方法模式
- SSM超强大整合,转载。。。
- [HDU 5507] GT and strings
- BlockCanary简介
- springmvc 拦截器
- OpenGL(二)