java访问webservce,保持会话,服务端保存session验证

在进行程序开发的过程中,遇到一个问题,怎么保持会话。

因为一帮进行方法调用很少涉及到即时身份验证的。

例如:

1:客户端登录后服务端保存登录用户信息;

2:客户端持有验证通过key再次请求;

3:服务端得到key对比session存储key进行身份验证。

问题来了,请求服务,请求完成后,为了节约服务器资源,会断开连接,服务端如何识别是哪次请求的session呢?

这个时候就需要在第一次请求时,服务端返回sessionkey ,客户端再次请求时再请求头部使用此sessionkey表面身份,

在java端的方法如下:

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; } }

 

你可能感兴趣的:(session)