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.
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.
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 .
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With