如何将cookie保存并返回给Web服务?

Kan*_*esh 6 android

我正在使用kso​​ap2进行Web服务方法调用.我使用ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar并能够从Web服务响应中检索标头值.我想保存任何返回的cookie,并随后调用Web服务返回它们.

我使用以下代码检索标头:

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();     
  }
Run Code Online (Sandbox Code Playgroud)

我试图将它保存在SharedPreferences中,但没有成功.我这么冷啊?请帮忙.

提前致谢.

Kan*_*esh 7

问题解决了.

要保存标题内容:

          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();
Run Code Online (Sandbox Code Playgroud)

根据要求设置cookie:

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

headerList.add(headerPropertyObj);

androidHttpTransport.call(SOAP_ACTION,envelope,headerList);