android联接webservice是cookies和session保持方法

android连接webservice是cookies和session保持方法

添加用android连接 php的 nusoap 做成的webservice是session和cookies不能保存,经过查找在国外的一个网站上找到了解决方法,具体实现方法如下(http://www.my400800.cn ):

 

取得请求页面cookies内容,等下次发送请求时在把cookies的内容发送过去

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;  SharedPreferences preferences =null;

    HttpTransportSE  androidHttpTransport  = new HttpTransportSE(URL);

      Editor sharedPreferenceEditor = preferences.edit();
      List headerList = androidHttpTransport.call(SOAP_ACTION, envelope, null);
      for (Object header : headerList) {
          HeaderProperty headerProperty = (HeaderProperty) header;
          String headerKey = headerProperty.getKey();
          String headerValue = headerProperty.getValue();
          System.out.println(headerKey +" : " + headerValue);
          sharedPreferenceEditor.putString(headerKey, headerValue);
      }
      sharedPreferenceEditor.commit();

把取得请求页面cookies内容在发送给下一个请求的页面,就实现了cookies内容的保持,代码如下:

 HeaderProperty headerPropertyObj = new HeaderProperty("cookie", preferences.getString("set-cookie", ""));

    headerList.add(headerPropertyObj);

    androidHttpTransport.call(SOAP_ACTION, envelope, headerList);