Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

android - How to save and return cookies to the web service?

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

  for (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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...