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,而且是 ...
随机推荐
- AES - Rijndael 算法(二)
三:Rijndael算法实现(C++版本) /*-------------------- Rijndael round subkeys ---------------------*/u8 roundK ...
- 在SQL中用正则表达式替换html标签
由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都替换掉.当然我可以通过写一个程序去修改,那毕竟有点麻烦.直接在查询分析器中执行,但是MS SQL Server并没有 ...
- 【Html 学习笔记】第三节——超链接
这一节看看超级链接的应用 普通超链接:<a href=""> <a> 第一个由于环境目前无法尝试,第二个点击后跳转到qq主页. 图片超链接:<imag ...
- kubernetes组件
kubernetes组件 @(马克飞象)[k8s] 组件 kubernetes除了必备的dns和网络组件外,官方推出大量的cluster-monitoring,dashboard,fluentd-el ...
- localStorage存储JSON对象的小方法
有时候,我们需要将数据存储到sessionStorage和localStorage中,这样做的好处有: 1 缓存数据 2 减少对内存的占用 但是,storage只能存储字符串的数据,对于JS中常用的数 ...
- web服务交互中HTTP数据内容GZIP,ZLIB格式压缩与解压缩封装(共享)
点击下载独立的dll //dll内部封装API格式 //gzip BOOL fnZlibDecompressPacket (__IN_PARAM unsigned char* gZlibDataBuf ...
- Android 自定义View (一)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
- Win32 的dll导入
dll 文件可以导入变量,函数,和C++类,但是导入变量会使执行程序与dll紧耦合,而C++类导入则需要两个文件的开发商所用的编译器相兼容,所以做好只导入函数; 创建dll : 头文件:#ifdef ...
- linux中内核的一个不错的参数somaxconn
导读:在linux中,/proc/sys/net/core/somaxconn这个参数,linux中内核的一个不错的参数somaxconn 看下其解析: 对于一个TCP连接,Server与Client ...
- Android Studio 安装
准备: JDK 7以及以上版本. Android Studio安装文件 中文站下载 http://www.android-studio.org/index.php/download exe ,包含S ...