歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

關於Android連接request和response中攜帶cookie

關於Android連接request和response中攜帶cookie:

  1. class LoginTask extends AsyncTask<String, Object, String> {  
  2.         @Override  
  3.         protected void onPreExecute() {  
  4.             progressDialog.setMessage("Loading...");  
  5.             progressDialog.show();  
  6.         }  
  7.   
  8.         @Override  
  9.         protected String doInBackground(String... par) {  
  10.             HttpPost post = new HttpPost(par[0]);  
  11.             params = new ArrayList<NameValuePair>();  
  12.             params.add(new BasicNameValuePair("methodName""login"));  
  13.             params.add(new BasicNameValuePair("username", username));  
  14.             params.add(new BasicNameValuePair("password", password));  
  15.             /**add cookie 
  16.              * Cookie cookie=new BasicClientCookie("","");  
  17.              * CookieSpecBase cookieSpecBase=new BrowserCompatSpec();  
  18.              * List<Cookie> cookies=new ArrayList<Cookie>();  
  19.              * cookies.add(cookie); 
  20.              * cookieSpecBase.formatCookies(cookies); 
  21.              * post.setHeader(cookieSpecBase.formatCookies(cookies).get(0)); 
  22.              */  
  23.             DefaultHttpClient client=new DefaultHttpClient();  
  24.             try {  
  25.                 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
  26.                 try {  
  27.                     res = client.execute(post);  
  28.                 } catch (Exception e) {  
  29.                     return "網絡連接錯誤,請檢查網絡是否打開!";  
  30.                 }  
  31.                 responseCode = res.getStatusLine().getStatusCode();  
  32.                 if (responseCode != 200)  
  33.                     return "服務器正忙! 返回代碼為:" + responseCode;  
  34.                 /** get cookie 
  35.                 * List<Cookie>  cookiess=client.getCookieStore().getCookies(); 
  36.                 * cookiess.get(0).getName(); 
  37.                 * cookiess.get(0).getValue(); 
  38.                 */  
  39.                 bis = new ByteArrayInputStream(EntityUtils.toByteArray(res  
  40.                         .getEntity()));  
  41.                 dis = new DataInputStream(bis);  
  42.                 c = new Customers();  
  43.                 if (dis.readUTF().equals("notnull")) {  
  44.                     c.setCustomerid(dis.readLong());  
  45.                     c.setUsername(dis.readUTF());  
  46.                     c.setPassword(dis.readUTF());  
  47.                     c.setRealname(dis.readUTF());  
  48.                     c.setIdentify(dis.readUTF());  
  49.                     c.setSex(dis.readUTF());  
  50.                     c.setBirthday(dis.readUTF());  
  51.                     c.setCustomeraddress(dis.readUTF());  
  52.                     c.setCreditcard(dis.readUTF());  
  53.                     c.setEmail(dis.readUTF());  
  54.                     c.setTelephone(dis.readUTF());  
  55.                     c.setAddress(dis.readUTF());  
  56.                     c.setZip(dis.readUTF());  
  57.                     c.setRemark(dis.readUTF());  
  58.                     wirteToFile(c.getUsername(), c.getPassword());  
  59.                 } else {  
  60.                     return "請重新輸入密碼或者用戶名不正確!";  
  61.                 }  
  62.   
  63.             } catch (UnsupportedEncodingException e) {  
  64.                 return "錯誤:UnsupportedEncodingException";  
  65.             } catch (IOException e) {  
  66.                 e.printStackTrace();  
  67.                 return "錯誤:讀取流出錯!";  
  68.             } finally {  
  69.                 try {  
  70.                     if (dis != null || bis != null) {  
  71.                         dis.close();  
  72.                         bis.close();  
  73.                     }  
  74.                 } catch (IOException e) {  
  75.                     return "關閉流失敗";  
  76.                 }  
  77.             }  
  78.             return null;  
  79.         }  
  80.   
  81.         @Override  
  82.         protected void onPostExecute(String result) {  
  83.             progressDialog.cancel();  
  84.             if (result != null) {  
  85.                 fail = true;  
  86.                 alertDialog(result);  
  87.             }  
  88.         }  
  89.     }  
Copyright © Linux教程網 All Rights Reserved