首页 > 编程知识 正文

手机谷歌浏览器cookie,http2取代cookie

时间:2023-05-06 03:21:21 阅读:111547 作者:577

安卓http客户端使用Cookie今天想把使用http客户端的自动登录小程序移植到安卓上。 安卓的SDK上有http客户端的包真是太好了。 查看Android文档时,官方还提供了实现Http客户端界面的Android Http客户端。 我在网上搜索也没有找到关于Android http客户端的文章。 当然,您可以继续使用default http客户端,但使用为Android定制的Android http客户端更自然。

以下是用于两个测试的http servlet。

publicclassloginextendshttp servlet {/* * * processesrequestsforbothhttp * code get/code and * code post/code methods.* @ @ paramresponseservletresponse * @ throwsservletexceptionifaservlet-specificerroroccurs * @ throwsioexceptionifani/oerrrororororororoce protectedvoidprocessrequest (httpservletrequestrequest,htpservletresponseresponse (throwsservletexception,io exception { charset=UTF-8 '; request.setcharacterencoding (utf-8 ); printwriter out=response.get writer (; htpsessionsession=request.getsession (; stringinfo=request.getparameter (info ); sssion.setattribute('info ',info ); try {/* todooutputyourpagehere.youmayusefollowingsamplecode.*/out.println (' ok ' ); } finally { out.close (); }//editor-folddefaultstate=' collapsed ' desc=' http servlet methods.clickonthesignonthelefttoeditthecode.'/* * coll @ paramrequestservletrequest * @ paramresponseservletresponse * @ throwsservletexceptionifaservlet-specificerrorocurs * @ ttttexconse/@ overrideprotectedvoiddoget (httpservletrequestrequest,HttpServletResponse response ) throws ServletException, io exception }/* * * handlesthehttp * code post/code method.* * @ paramrequestservletrequest * @ paramresponseseservletrespont @ throwsservletexceptionifaservlet-specificerroroccurs * @ throwsioexceptionifani/oerroroccurs */@ overrideprotectedvoid

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不然不能创建新实例。

posted on 2012-11-26 20:16 Kagami 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kagami/archive/2012/11/26/2789691.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。