近期須要實現一個功能,就是須要經過發送短信進行註冊,如今想把短信驗證碼放到服務器的session值中,當客戶端收到短信並提交短信碼時由asp.net服務端進行判斷,那麼如何共享這個session那麼須要在android客戶端中添加幾行代碼。以下操做。第一次數據請求時就獲取這個cookie的名稱而且獲得這個cookie的值,這個便是sessionid的值並保存在一個靜態變量中,而後在第二次請求數據的時候要將這個sessionid一併放在Cookie中發給服務器,服務器則是經過這個sessionid來識別到底是那個客戶端在請求數據的,在asp.net中這個sessionid的名字叫作ASP.NET_SessionId,固然咱們能夠從程序中獲取,以下代碼:android
//獲取服務端的這個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 cookie
}session
獲取sessionid的值app
for(int i=0;i<cookies.size();i++){ asp.net
if("ASP.NET_SessionId".equals(cookies.get(i).getName())){
JSESSIONID = cookies.get(i).getValue(); //這個即爲sessionid的值
break;
} post
}url
完整的httputils代碼以下:spa
public static DefaultHttpClient httpClient = null;.net
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;
}