AndroidHttpClient使用Cookie应用分析
程序员文章站
2023-08-17 17:01:44
今天想把一个用使用了httpclient的自动签到小程序移植到android上,还好android的sdk自带了httpclient的包。翻android的文档时发现官方还...
今天想把一个用使用了httpclient的自动签到小程序移植到android上,还好android的sdk自带了httpclient的包。翻android的文档时发现官方还提供了一个实现了httpclient接口的androidhttpclient,上网搜了下没发现关于androidhttpclient的文章。当然也可以继续使用defaulthttpclient,但用为android定制的androidhttpclient自然更好。
下面是2个测试用的httpservlet:
public class login extends httpservlet {
/**
* processes requests for both http
* <code>get</code> and
* <code>post</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
protected void processrequest(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
request.setcharacterencoding("utf-8");
printwriter out = response.getwriter();
httpsession session=request.getsession();
string info=request.getparameter("info");
session.setattribute("info", info);
try {
/* todo output your page here. you may use following sample code. */
out.println("ok");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on the + sign on the left to edit the code.">
/**
* handles the http
* <code>get</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* handles the http
* <code>post</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* returns a short description of the servlet.
*
* @return a string containing servlet description
*/
@override
public string getservletinfo() {
return "short description";
}// </editor-fold>
}
public class info extends httpservlet {
/**
* processes requests for both http
* <code>get</code> and
* <code>post</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
protected void processrequest(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
printwriter out = response.getwriter();
httpsession session=request.getsession();
string info=(string)session.getattribute("info");
try {
/* todo output your page here. you may use following sample code. */
if(info==null)
out.print("null");
else
out.print(info);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on the + sign on the left to edit the code.">
/**
* handles the http
* <code>get</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* handles the http
* <code>post</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* returns a short description of the servlet.
*
* @return a string containing servlet description
*/
@override
public string getservletinfo() {
return "short description";
}// </editor-fold>
}
主要代码在processrequest里,其他可以不用看。
访问login时传一个name为info的值,这时浏览器会得到一个用于定位服务端session的cookie。然后访问info,如果有cookie的话服务端能找到刚才你传的值并返回给你,没带cookie的话就不能找到。
android端代码:
public class mainactivity extends activity {
private androidhttpclient mhttpclient=androidhttpclient.newinstance("");
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
findviewbyid(r.id.button1).setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
new thread(rtest).start();
}
});
}
private string tostring(inputstream is) throws ioexception{
string ret="";
inputstreamreader isr=new inputstreamreader(is);
bufferedreader br=new bufferedreader(isr);
string tmp=br.readline();
while(tmp!=null){
ret+=tmp;
tmp=br.readline();
}
br.close();
return ret;
}
private runnable rtest=new runnable() {
@override
public void run() {
// todo auto-generated method stub
try {
basichttpcontext context=new basichttpcontext();
context.setattribute(clientcontext.cookie_store,new basiccookiestore());
httppost httppost = new httppost("http://10.226.233.48:8080/webapplication1/login");
list <namevaluepair> nvps = new arraylist <namevaluepair>();
nvps.add(new basicnamevaluepair("info", "你好 世界!!"));
httppost.setentity(new urlencodedformentity(nvps,"utf-8"));
httpresponse response=mhttpclient.execute(httppost,context);
httpentity entity = response.getentity();
log.i("kagami", mainactivity.this.tostring(entity.getcontent()));
entity.consumecontent();
httpget httpget2 = new httpget("http://10.226.233.48:8080/webapplication1/info");
httpresponse response2=mhttpclient.execute(httpget2,context);
httpentity entity2 = response2.getentity();
log.i("kagami", mainactivity.this.tostring(entity2.getcontent()));
entity2.consumecontent();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
};
}
androidhttpclient和defaulthttpclient的区别:
androidhttpclient不能在主线程中execute,会抛出异常。androidhttpclient通过静态方法newinstance获得实例,参数是代理,不用代理的话填“”。defaulthttpclient默认是启用cookie的,androidhttpclient默认不启用cookie,要使用的话每次execute时要加一个httpcontext参数,并且添加cookiestore。用完后别忘了close不然不能创建新实例。
下面是2个测试用的httpservlet:
复制代码 代码如下:
public class login extends httpservlet {
/**
* processes requests for both http
* <code>get</code> and
* <code>post</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
protected void processrequest(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
request.setcharacterencoding("utf-8");
printwriter out = response.getwriter();
httpsession session=request.getsession();
string info=request.getparameter("info");
session.setattribute("info", info);
try {
/* todo output your page here. you may use following sample code. */
out.println("ok");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on the + sign on the left to edit the code.">
/**
* handles the http
* <code>get</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* handles the http
* <code>post</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* returns a short description of the servlet.
*
* @return a string containing servlet description
*/
@override
public string getservletinfo() {
return "short description";
}// </editor-fold>
}
复制代码 代码如下:
public class info extends httpservlet {
/**
* processes requests for both http
* <code>get</code> and
* <code>post</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
protected void processrequest(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
printwriter out = response.getwriter();
httpsession session=request.getsession();
string info=(string)session.getattribute("info");
try {
/* todo output your page here. you may use following sample code. */
if(info==null)
out.print("null");
else
out.print(info);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on the + sign on the left to edit the code.">
/**
* handles the http
* <code>get</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* handles the http
* <code>post</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws servletexception if a servlet-specific error occurs
* @throws ioexception if an i/o error occurs
*/
@override
protected void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
processrequest(request, response);
}
/**
* returns a short description of the servlet.
*
* @return a string containing servlet description
*/
@override
public string getservletinfo() {
return "short description";
}// </editor-fold>
}
主要代码在processrequest里,其他可以不用看。
访问login时传一个name为info的值,这时浏览器会得到一个用于定位服务端session的cookie。然后访问info,如果有cookie的话服务端能找到刚才你传的值并返回给你,没带cookie的话就不能找到。
android端代码:
复制代码 代码如下:
public class mainactivity extends activity {
private androidhttpclient mhttpclient=androidhttpclient.newinstance("");
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
findviewbyid(r.id.button1).setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
new thread(rtest).start();
}
});
}
private string tostring(inputstream is) throws ioexception{
string ret="";
inputstreamreader isr=new inputstreamreader(is);
bufferedreader br=new bufferedreader(isr);
string tmp=br.readline();
while(tmp!=null){
ret+=tmp;
tmp=br.readline();
}
br.close();
return ret;
}
private runnable rtest=new runnable() {
@override
public void run() {
// todo auto-generated method stub
try {
basichttpcontext context=new basichttpcontext();
context.setattribute(clientcontext.cookie_store,new basiccookiestore());
httppost httppost = new httppost("http://10.226.233.48:8080/webapplication1/login");
list <namevaluepair> nvps = new arraylist <namevaluepair>();
nvps.add(new basicnamevaluepair("info", "你好 世界!!"));
httppost.setentity(new urlencodedformentity(nvps,"utf-8"));
httpresponse response=mhttpclient.execute(httppost,context);
httpentity entity = response.getentity();
log.i("kagami", mainactivity.this.tostring(entity.getcontent()));
entity.consumecontent();
httpget httpget2 = new httpget("http://10.226.233.48:8080/webapplication1/info");
httpresponse response2=mhttpclient.execute(httpget2,context);
httpentity entity2 = response2.getentity();
log.i("kagami", mainactivity.this.tostring(entity2.getcontent()));
entity2.consumecontent();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
};
}
androidhttpclient和defaulthttpclient的区别:
androidhttpclient不能在主线程中execute,会抛出异常。androidhttpclient通过静态方法newinstance获得实例,参数是代理,不用代理的话填“”。defaulthttpclient默认是启用cookie的,androidhttpclient默认不启用cookie,要使用的话每次execute时要加一个httpcontext参数,并且添加cookiestore。用完后别忘了close不然不能创建新实例。
上一篇: java开发_图片截取工具实现原理
下一篇: java 静态代理 动态代理深入学习
推荐阅读
-
深入分析iOS应用中对于图片缓存的管理和使用
-
AndroidHttpClient使用Cookie应用分析
-
深入分析iOS应用中对于图片缓存的管理和使用
-
AndroidHttpClient使用Cookie应用分析
-
PHP swoole中使用task进程异步的处理耗时任务应用案例分析
-
PHP cookie,session的使用与用户自动登录功能实现方法分析
-
caliper的应用之二Solo版本的分析使用
-
应用负载均衡之LVS(三):使用ipvsadm以及详细分析VS/DR模式
-
Django cookie和session的应用场景及如何使用
-
使用 MiniProfiler 来分析 ASP.NET Core 应用