Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and return cookies to the web service?

Tags:

android

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.

like image 317
Kannan Suresh Avatar asked Jun 17 '11 04:06

Kannan Suresh


People also ask

How is cookie sent back to the Web server?

Cookies are sent by the browser to the server when an HTTP request starts, and they are sent back from the server, which can edit their content. Cookies are essentially used to store a session id. In the past cookies were used to store various types of data, since there was no alternative.

How are cookies returned?

When cookies are sent back to the server, they are read, (base64) decoded, decrypted, JSON parsed, and stored in memory as key/value pairs. This is how sessions work in Lucky. The session will be a cookie with a name like _myapp_session .

How do I store cookies in HTTP?

The browser usually stores the cookie and sends it with requests made to the same server inside a Cookie HTTP header. You can specify an expiration date or time period after which the cookie shouldn't be sent. You can also set additional restrictions to a specific domain and path to limit where the cookie is sent.

Are cookies stored on the Web server?

The server only sends the cookie when it wants the web browser to save it. If you're wondering “where are cookies stored,” it's simple: your web browser will store it locally to remember the “name-value pair” that identifies you.


1 Answers

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

like image 137
Kannan Suresh Avatar answered Nov 14 '22 23:11

Kannan Suresh