java訪問webservce,保持會話,服務端保存session驗證

在進行程序開發的過程當中,遇到一個問題,怎麼保持會話。java

由於一幫進行方法調用不多涉及到即時身份驗證的。web

例如:服務器

1:客戶端登陸後服務端保存登陸用戶信息;session

2:客戶端持有驗證經過key再次請求;ide

3:服務端獲得key對比session存儲key進行身份驗證。this

問題來了,請求服務,請求完成後,爲了節約服務器資源,會斷開鏈接,服務端如何識別是哪次請求的session呢?url

這個時候就須要在第一次請求時,服務端返回sessionkey ,客戶端再次請求時再請求頭部使用此sessionkey表面身份,spa

在java端的方法以下:.net

public class WSHelper implements Runnable {
    private static ExecutorService pool =  Executors.newSingleThreadExecutor();

    private static final String TAG = "WSHelper";

    // 設置webservices的超時時長
    private static final int ACCESS_TIMEOUT = 15000;
    
    private static String sessionId ;
    
    private WSParams mPs;
    private WSCallBack mCallback;

    
    public static void setSessionId(String id){
        sessionId = id;
    }
    
    private WSHelper( WSParams ps , WSCallBack callback){
        mPs = ps;
        mCallback = callback;
    }
    //namespace:命名空間(http://www.ulife.com.cn/)
//
methodName:方法名稱
//url:服務調用地址(http://192.168.0.250/WebService/UHomeStationService.asmx)
//params:參數

public static Object callWebService(String nameSpace, String methodName, String url, Map<String, Object> params) {
final String soapAction = nameSpace + methodName; Log.e(TAG, "#開始調用WebServices → nameSpace= " + nameSpace + "; methodName= " + methodName + " url= " + url + "; sessionId="+ sessionId +"; params= " + params); // 1.建立數據對象 參數1:webServices命名空間 2:要訪問的WebServices名稱
        SoapObject request = new SoapObject(nameSpace, methodName); // 2.建立信封對象
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; // .net 支持
        envelope.bodyOut = request; // 添加輸出參數 // 添加要傳遞的websercice的參數
        if (params != null && !params.isEmpty()) { for (Iterator<Entry<String, Object>> it = params.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> e = it.next(); request.addProperty(e.getKey(), e.getValue()); } } // 3.建立運輸機對象對象
        HttpTransportSE httpTransportSE = new HttpTransportSE( url , ACCESS_TIMEOUT ); httpTransportSE.debug = true; Object object = null; try {  List<HeaderProperty> headers = null; if ( sessionId != null ){ headers = new ArrayList<HeaderProperty>(); headers.add(new HeaderProperty("Cookie" , "ASP.NET_SessionId=" + sessionId)); }  // 4.開始請求WebServices 參數1爲 Action地址 (命名空間 + WebServices名稱) 參數2爲 信封對象
 httpTransportSE.call(soapAction, envelope , headers);        // 5 .得到WebServices返回數據
            object = envelope.getResponse(); } catch (Exception e) { e.printStackTrace(); } Log.e(TAG, "###WebServices → result = " + object); return object; } public static void callWS( WSParams ps , WSCallBack callback){ new Thread(new WSHelper(ps , callback)).start(); } public static void callWSSingleThread( WSParams ps , WSCallBack callback){ pool.execute(new WSHelper(ps , callback)); } public static boolean checkNetAndCallWS(Context context, WSParams ps, WSCallBack callback){ if ( NetUtil.checkNetwork(context)){ new Thread(new WSHelper(ps , callback)).start(); } else { Toast.makeText(context, R.string.wang_luo_bu_ke_yong, Toast.LENGTH_SHORT).show(); return false; } return true; } public static class WSParams { public WSParams(String nameSpace, String methodName, String url, Map<String, Object> params){ this.nameSpace = nameSpace; this.methodName = methodName; this.url = url; this.params = params; } public String nameSpace; public String methodName; public String url; public Map<String, Object> params; } public interface WSCallBack{ void callBack(Object obj); } @Override public void run() { try { Object obj = callWebService(mPs.nameSpace , mPs.methodName , mPs.url , mPs.params); if ( mCallback != null ) mCallback.callBack(obj); } catch (Exception e) { Log.e(TAG, "callback error!", e); } } public static String filterValue(Object value){ String str = null; if ( value != null ){ str = value.toString(); if ( "".equals(str) || "anyType{}".equals( str ) ) str = null ; } return str; } }
相關文章
相關標籤/搜索