开发者

How to save and return cookies to the web service?

开发者 https://www.devze.com 2023-03-13 10:04 出处:网络
I\'m using ksoap2 for web service method calls. I used ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar and was able to retrieve header values from the web service response. I would like to sav

I'm using ksoap2 for web service method calls. I used ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar and was able to retrieve header values from the web service response. I would like to save any returned cookies and return them with subsequent calls to the web service.

I retrieved the header using the following code:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;

  envelope.setOutputSoapObject(request);

  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

  List headerList = androidHttpTransport.call(SOAP_ACTION, envelope, null);

  f开发者_运维技巧or (Object header : headerList) {
      HeaderProperty headerProperty = (HeaderProperty) header;
      String headerKey = headerProperty.getKey();
      String headerValue = headerProperty.getValue();     
  }

I tried to save it in SharedPreferences, but was unsuccessful. How cold I do this? Please help.

Thanks in advance.


Issue solved.

To save the header contents:

          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();

To set the cookie on request:

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

headerList.add(headerPropertyObj);

androidHttpTransport.call(SOAP_ACTION, envelope, headerList);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号