android中如何处理cookie
Managing Cookies
HttpClient provides cookie management features that can be particularly useful to test the way an application handles cookies. Listing 9-3 shows an example where you use HttpClient to add a cookie to a request and also to list details of cookies set by the JSP you invoke using the HttpClient code.
The HttpState class plays an important role while working with cookies. The HttpState class works as a container for HTTP attributes such as cookies that can persist from one request to another. When you normally surf the Web, the Web browser is what stores the HTTP attributes.
Listing 9-3. CookiesTrial.java
package com.commonsbook.chap9;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod; public class CookiesTrial { private static String url =
"http://127.0.0.1:8080/HttpServerSideApp/CookieMgt.jsp"; public static void main(String[] args) throws Exception { //A new cookie for the domain 127.0.0.1
//Cookie Name= ABCD Value=00000 Path=/ MaxAge=-1 Secure=False
Cookie mycookie = new Cookie("127.0.0.1", "ABCD", "00000", "/", -1, false); //Create a new HttpState container
HttpState initialState = new HttpState();
initialState.addCookie(mycookie); //Set to COMPATIBILITY for it to work in as many cases as possible
initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
//create new client
HttpClient httpclient = new HttpClient();
//set the HttpState for the client
httpclient.setState(initialState); GetMethod getMethod = new GetMethod(url);
//Execute a GET method
int result = httpclient.executeMethod(getMethod); System.out.println("statusLine>>>"+getMethod.getStatusLine()); //Get cookies stored in the HttpState for this instance of HttpClient
Cookie[] cookies = httpclient.getState().getCookies(); for (int i = 0; i < cookies.length; i++) {
System.out.println("nCookieName="+cookies[i].getName());
System.out.println("Value="+cookies[i].getValue());
System.out.println("Domain="+cookies[i].getDomain());
} getMethod.releaseConnection();
}
}
In Listing 9-3, you use the HttpState instance to store a new cookie and then associate this instance with theHttpClient instance. You then invoke CookieMgt.jsp. This JSP is meant to print the cookies it finds in the request and then add a cookie of its own. The JSP code is as follows:
<%
Cookie[] cookies= request.getCookies(); for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].getName() +" = "+cookies[i].getValue());
} //Add a new cookie
response.addCookie(new Cookie("XYZ","12345"));
%>
CAUTION HttpClient code uses the class org.apache.commons.httpclient.Cookie, and JSP and servlet code uses the class javax.servlet.http.Cookie.
The output on the application console upon executing the CookiesTrial class and invoking CookieMgt.jsp is as follows:
statusLine>>>HTTP/1.1 200 OK CookieName=ABCD
Value=00000
Domain=127.0.0.1 CookieName=XYZ
Value=12345
Domain=127.0.0.1 CookieName=JSESSIONID
Value=C46581331881A84483F0004390F94508
Domain=127.0.0.1
In this output, note that although the cookie named ABCD has been created from CookiesTrial, the other cookie named XYZ is the one inserted by the JSP code. The cookie named JSESSIONID is meant for session tracking and gets created upon invoking the JSP. The output as displayed on the console of the server when the JSP is executed is as follows:
ABCD = 00000
This shows that when CookieMgt.jsp receives the request from the CookiesTrial class, the cookie namedABCD was the only cookie that existed. The sidebar “HTTPS and Proxy Servers” shows how you should handle requests over HTTPS and configure your client to go through a proxy.
|
You will now see the HttpClient component’s capability to use MultipartPostMethod to upload multiple files. You will look at this in tandem with the Commons FileUpload component. This Commons component is specifically meant to handle the server-side tasks associated with file uploads.
Introducing FileUpload
The FileUpload component has the capability of simplifying the handling of files uploaded to a server. Note that the FileUpload component is meant for use on the server side; in other words, it handles where the files are being uploaded to—not the client side where the files are uploaded from. Uploading files from an HTML form is pretty simple; however, handling these files when they get to the server is not that simple. If you want to apply any rules and store these files based on those rules, things get more difficult.
The FileUpload component remedies this situation, and in very few lines of code you can easily manage the files uploaded and store them in appropriate locations. You will now see an example where you upload some files first using a standard HTML form and then using HttpClient code.
android中如何处理cookie的更多相关文章
- 【WebView】Android WebView中的Cookie操作
Hybrid App(混合式应用)的开发过程中少不了与WebView的交互,在涉及到账户体系的产品中,包含了一种登录状态的传递.比如,在Native(原生)界面的登录操作,进入到Web界面时,涉及到账 ...
- 如何处理Android中的防缓冲区溢出技术
[51CTO专稿]本文将具体介绍Android中的防缓冲区溢出技术的来龙去脉. 1.什么是ASLR? ASLR(Address space layout randomization)是一种针对缓冲区溢 ...
- Token在android中的使用
首先Token是一个怎么样的东西,Token存在的意义又在哪里?学过php或是其他web开发的人都知道一个东西叫session和cookie,这些东西可以在服务器或是本地保存一些东西,比如说登录状态, ...
- Android中的HTTP通信
前言:近期在慕课网学习了慕课网课程Android中的HTTP通信,就自己总结了一下,其中参考了不少博文,感谢大家的分享. 文章内容包括:1.HTTP简介2.HTTP/1.0和HTTP/1.1之间的区别 ...
- Android中事件传递机制的总结
事件传递虽然算不上某个单独的知识点,但是在实际项目开发中肯定会碰到,如果不明白其中的原理,那在设计各种滑动效果时就会感到很困惑. 关于事件的传递,我们可能会有以下疑问: 事件是如何传递的 事件是如何处 ...
- Android中对Log日志文件的分析[转]
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...
- 系统剖析Android中的内存泄漏
[转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...
- Android中的Intent详解
前言: 每个应用程序都有若干个Activity组成,每一个Activity都是一个应用程序与用户进行交互的窗口,呈现不同的交互界面.因为每一个Acticity的任务不一样,所以经常互在各个Activi ...
- Android中的SQLite使用学习
Android中的SQLite使用学习 SQLite是非常流行的嵌入式关系型数据库,轻载, 速度快,而且是开源.在Android中,runtime提供SQLite,所以我们可以使用SQLite,而且是 ...
随机推荐
- Linux 下最为人熟知的解压缩工具
很多时候,通过互联网发送或接收大文件和图片是一件令人头疼的事.压缩及解压缩工具正好可以应对这个问题.下面让我们快速浏览一些可以使得我们的工作更加轻松的开源工具. Tar Tar 由 ‘Tape arc ...
- linux内核--自旋锁的理解
http://blog.chinaunix.net/uid-20543672-id-3252604.html 自旋锁:如果内核配置为SMP系统,自旋锁就按SMP系统上的要求来实现真正的自旋等待,但是对 ...
- 解决多线程下simpleDateFormat的安全问题
// 日期格式化 private static final ThreadLocal<SimpleDateFormat> GMT_FORMATERS = new ThreadLocal< ...
- 使用PHPExcel导出数据
最近要求做增加客流数据等导出为Excel的功能,phpExcel包功能强大,根据实际需求,我只学习了简单的功能. 安装PHPExcel 在composer.json中添加: "require ...
- Linux操作系统Centos7.2版本搭建Apache+PHP+Mysql环境
对于在校大学生来说腾讯云1元主机很划算,所以就申请了一台,打算在上面学习下linux,使用版本为centos7.2版本.在服务器上比较推荐centos,此版本生命周期较长,而且网上有关centos的教 ...
- OpenStack Networking
今天的数据中心网络比以往不论什么时候包括的设备都要多,比如server.网络设备.存储系统和安全设备等.这当中有非常多被近一步划分为多个虚拟机和虚拟网络.IP地址的数量.路由配置和安全规则能够迅速达到 ...
- Android常用ProgressDialog设置
public static ProgressDialog initDialog(Context context) { ProgressDialog progressDialog = new Progr ...
- 一个int类型究竟占多少个字节
一个int占多少个字节? 这个问题我们往往得到的答案是4. 可是int究竟占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes ...
- 关于虚拟继承类的大小问题探索,VC++ 和 G++ 结果是有区别的
昨天笔试遇到个 关于类占用的空间大小的问题,以前没怎么重视,回来做个试验,还真发现了问题,以后各位笔试考官门,出题时请注明是用什么编译器. vc6/vc8 cl 和 Dev-C 的g++ 来做的测试: ...
- 通过编写一个简单的漏洞扫描程序学习Python基本语句
今天开始读<Python绝技:运用Python成为顶级黑客>一书,第一章用一个小例子来讲解Python的基本语法和语句.主要学习的内容有:1. 安装第三方库.2. 变量.字符串.列表.词典 ...