上一阶段项目设计使用cookie信息实现登录访问功能,在实现过程遇到一些问题,下面整理一下:

首先,client想使用cookie,必须访问一次server从会话中获取cookie信息,然后在设置回去,在android使用HttpURLConnection 直接设置会报异常

查阅文档及StackOver发现android需要使用CookieManager进行处理cookie相关信息,实现如下:

 InputStream input = null;
OutputStream output = null; HttpURLConnection connection = null;
try {
java.net.CookieManager manager = new java.net.CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager); URL url = new URL(dnUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.connect(); connection.getHeaderFields();
CookieStore store = manager.getCookieStore(); int resultCode=connection.getResponseCode();
responseUpdateCookieHttpURL(store);
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (resultCode != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
  /**
* 更新本地Cookie信息
*/
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void responseUpdateCookieHttpURL(CookieStore store) {
boolean needUpdate = false;
List<HttpCookie> cookies = store.getCookies();
HashMap<String, String> cookieMap = null;
if (cookieMap == null) {
cookieMap = new HashMap<String, String>();
}
for (HttpCookie cookie : cookies) {
String key = cookie.getName();
String value = cookie.getValue();
if (cookieMap.size() == 0 || !value.equals(cookieMap.get(key))) {
needUpdate = true;
}
cookieMap.put(key, value);
// BDebug.e(HTTP_COOKIE, cookie.getName() + "---->" + cookie.getDomain() + "------>" + cookie.getPath());
} }
 public static final int GET = 0;
public static final int POST = 1;
public static final String HTTP_POST_BODY = "body";
public static final String HTTP_COOKIE = "Cookie";
public static final String HTTP_USER_AGENT = "User-Agent";

HttpClient实现更改设置Cookie信息:

void handleCookie(String url){

		try{
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = client.execute(httpget); int responseCode = httpResponse.getStatusLine().getStatusCode(); HttpBuilder.responseUpdateCookieHttpClient((DefaultHttpClient)client); if (responseCode == HttpStatus.SC_OK) {
/*result = EntityUtils.toString(httpResponse.getEntity());
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();*/
}
}catch(Exception e){
e.printStackTrace();
}
}
   /**
* 获取cookie信息
*
* @param cookieMap
* @return
*/
public static String getCookieInfo(HashMap<String, String> cookieMap) {
StringBuilder cookieInfo = new StringBuilder();
if (cookieMap != null && cookieMap.size() > 0) {
Iterator<Entry<String, String>> iter = cookieMap.entrySet().iterator();
Entry<String, String> entry;
while (iter.hasNext()) {
String key = "";
String value = "";
entry = iter.next();
key = entry.getKey();
value = entry.getValue();
cookieInfo.append(key).append("=").append(value).append(";");
}
}
return cookieInfo.toString();
} /**
* 更新本地Cookie信息
*
* @param defaultHttpClient
*/
public static void responseUpdateCookieHttpClient(DefaultHttpClient defaultHttpClient) {
boolean needUpdate = false;
List<Cookie> cookies = defaultHttpClient.getCookieStore().getCookies();
HashMap<String, String> cookieMap = null;
if (cookieMap == null) {
cookieMap = new HashMap<String, String>();
}
for (Cookie cookie : cookies) {
String key = cookie.getName();
String value = cookie.getValue();
if (cookieMap.size() == 0 || !value.equals(cookieMap.get(key))) {
needUpdate = true;
}
cookieMap.put(key, value);
} }

基本就这些,有问题留言。

最新文章

  1. javascript变量的作用域
  2. 自学 web 前端人怎么找工作?
  3. 使用tomcat部署jsp程序
  4. Simptip – 使用 Sass 制作的 CSS Tooltip 效果
  5. 2078 Problem H Secret Message 中石油-未提交--&gt;已提交
  6. iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
  7. WebSocket 是什么原理?为什么可以实现持久连接?
  8. 1----lua的环境搭建
  9. SPOJ - DQUERY 主席树
  10. 打造轻量级自动化测试框架WebZ
  11. 服务器是windows时tomcat无法打印所有日志配置修改
  12. spring 内部工作机制(二)
  13. 2019/3/4 java集合学习(二)
  14. SQL Server 执行计划解析
  15. nginx worker_processes 配置
  16. Java之收集很好的Java学习资料地址+博客
  17. CCF-权限查询-201612-3
  18. 原生NodeJs制作一个简易聊天室
  19. Dom4j向XML中增加节点与属性——(四)
  20. 图-&gt;遍历

热门文章

  1. Linux - tar 命令详解 (压缩,解压,加密压缩,解密压缩)
  2. iOS Charles抓包
  3. 如何将PDF文件中的部分信息隐藏或遮盖呢?
  4. CodeGym自学笔记02——打印命令
  5. AWG含义及尺寸电流对照表-转载
  6. 如何在mac上配置Apache服务器
  7. Unity模型剖切
  8. ASP.NET Core实现自定义中间件的三种方式
  9. Pytorch实战学习(七):高级CNN
  10. 模拟ATM系统 —— 用户注册、登录和用户操作页设计 、查询账号、退出账号功能