HTTP请求使用CMWAP接入点的设置和会话保持
- 使用CMWAP接入点进行HTTP请求
需要在请求参数中加入代理,地址为10.0.0.172,端口为80
HttpParams baseParams = new BasicHttpParams(); baseParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("10.0.0.172", 80)); DefaultHttpClient httpclient = new DefaultHttpClient(baseParams);
HttpGet httpget = new HttpGet(url);
- HTTP请求中的会话保持
通过保存和设置HTTP请求的cookie来实现,大多网站只要保存和设置sessionid(cookie中名字为JSESSIONIDWAP)即可,但有的网站还有一些其他的参数,所以可以用一个HASHMAP来存储。
HashMap cookieMap = new HashMap()用来存储每次请求后返回的COOKIE值,存储方法随意,静态变量、数据库都可以。
StringBuffer cookieString = new StringBuffer();
Iterator iterator = cookieMap.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); Object value = entry.getValue(); Object key = entry.getKey(); cookieString.append(key).append("=").append(value); if (iterator.hasNext()) { cookieString.append(";"); } } httpget.setHeader("cookie", cookieString.toString());//设置cookie值 ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); List cookies = httpclient.getCookieStore().getCookies();//获取、保存cookie if (cookies.isEmpty()) { Log.i(ConfigUtil.LOGTAG, "cookies = none"); } else { cookieMap.clear(); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = (Cookie) cookies.get(i); Log.i(ConfigUtil.LOGTAG, ((Cookie) cookies.get(i)).getName() + "::::" + ((Cookie) cookies.get(i)).getValue()); cookieMap.put(cookie.getName(), cookie.getValue()); } }