国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android與asp.net服務端共享session的方法詳解

2019-10-22 18:28:22
字體:
來源:轉載
供稿:網友

前言

最近因為工作的需要,要實現一個功能,就是需要通過發送短信進行注冊,現在想把短信驗證碼放到服務器的session值中,當客戶端收到短信并提交短信碼時由asp.net服務端進行判斷,那么如何共享這個session那么需要在android客戶端中添加幾行代碼。

實現方法

如下操作。第一次數據請求時就獲取這個cookie的名稱并且得到這個cookie的值,這個即是sessionid的值并保存在一個靜態變量中,然后在第二次請求數據的時候要將這個sessionid一并放在Cookie中發給服務器,服務器則是通過這個sessionid來識別究竟是那個客戶端在請求數據的,在asp.net中這個sessionid的名字叫做ASP.NET_SessionId,當然我們可以從程序中獲取。

如下代碼:

//獲取服務端的這個sessionid的名稱/* 獲取cookieStore */ List<Cookie> cookies = cookieStore.getCookies(); for(int i=0;i<cookies.size();i++){   String sessionid = cookies.get(i).getName(); 從這里可以獲取到這個sessionid,即為 ASP.NET_SessionId}

獲取sessionid的值

for(int i=0;i<cookies.size();i++){if("ASP.NET_SessionId".equals(cookies.get(i).getName())){   JSESSIONID = cookies.get(i).getValue(); //這個即為sessionid的值  break; }}

完整的httputils代碼如下:

public static DefaultHttpClient httpClient = null; private static String JSESSIONID; //定義一個靜態的字段,保存sessionID  public static String getRequest(String url) { httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(url); try { HttpParams params = new BasicHttpParams();  HttpConnectionParams.setConnectionTimeout(params, 10000);//設置連接超時  HttpConnectionParams.setSoTimeout(params, 15000);//設置請求超時  get.setParams(params);  get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");  if(null != JSESSIONID){   get.setHeader("Cookie", "ASP.NET_SessionId="+JSESSIONID);   }  //連接響應,通過HttpResponse獲得響應信息 HttpResponse httpResponse = httpClient.execute(get); if(httpResponse.getStatusLine().getStatusCode() == 200) { //取得響應字符串 String result = EntityUtils.toString(httpResponse.getEntity()); return result; } } catch (ClientProtocolException e) { return null; } catch (IOException e) { return null; } return null; }  public static String postRequest(String url, HashMap<String, String> rawParams) throws Exception { httpClient = new DefaultHttpClient(); //創建POST請求方式 HttpPost post = new HttpPost(url);  HttpParams cparams = new BasicHttpParams();  HttpConnectionParams.setConnectionTimeout(cparams, 10000);//設置連接超時 HttpConnectionParams.setSoTimeout(cparams, 15000);//設置請求超時 post.setParams(cparams); post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); if(null != JSESSIONID){  post.setHeader("Cookie", "ASP.NET_SessionId="+JSESSIONID);   } //使用NameValuePair來保存要傳遞的參數,可以使用BasicNameValuePair來構造一個要被傳遞的參數 //通過add添加這個參數到NameValuePair中 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); for(String key : rawParams.keySet()) { //添加要傳傳遞的參數 params.add(new BasicNameValuePair(key, rawParams.get(key))); } //post需要為參數設置字符集 HttpEntity httpEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8); //請求httpRequest post.setEntity(httpEntity); //發送POST請求并獲取響應 HttpResponse httpResponse = null; try {  httpResponse = httpClient.execute(post); } catch(Exception ex) { String ee = ex.getMessage(); } if(httpResponse.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); /* 獲取cookieStore        ASP.NET_SessionId就是通過上面的方法獲取到?!     ?/    CookieStore cookieStore = httpClient.getCookieStore();    List<Cookie> cookies = cookieStore.getCookies();   for(int i=0;i<cookies.size();i++){     if("ASP.NET_SessionId".equals(cookies.get(i).getName())){      JSESSIONID = cookies.get(i).getValue();      break;     }    }  return result; } return null; }

android,session,asp.net,共享

android,session,asp.net,共享

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 都兰县| 阿勒泰市| 莱西市| 灵台县| 宁安市| 孝昌县| 涡阳县| 荔波县| 登封市| 濉溪县| 安溪县| 阜南县| 新乡县| 临海市| 江源县| 科技| 佛山市| 白山市| 沙田区| 炉霍县| 青浦区| 巫溪县| 景宁| 南通市| 汤原县| 南皮县| 阿拉善盟| 潞西市| 定日县| 连南| 长垣县| 辽源市| 尼勒克县| 玉龙| 泰州市| 合肥市| 泰州市| 科技| SHOW| 通海县| 遵义县|