manish-kukreja / ksoap2-android

Automatically exported from code.google.com/p/ksoap2-android
0 stars 0 forks source link

add ServiceConnectionSE method getServiceConnection so we can use cookie #25

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
we should use cookie for save session 
we must access URLConnection so you must 
add ServiceConnectionSE method getServiceConnection so we can use cookie
full code below

package com.glenet.androidWfLib.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.ServiceConnectionSE;
import org.xmlpull.v1.XmlPullParserException;

import com.sonalb.net.http.cookie.Client;
import com.sonalb.net.http.cookie.CookieJar;

public class newAndroidHttpTransport extends AndroidHttpTransport {

    private ServiceConnectionSE connection;

    public newAndroidHttpTransport(String url) {
        super(url);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected synchronized ServiceConnectionSE getServiceConnection()
            throws IOException {
        // TODO Auto-generated method stub
        // return super.getServiceConnection();
        if (connection == null) {
            connection = new ServiceConnectionSE(url);
            return connection;
        } else {

            return connection;
        }
    }

    public CookieJar call(String soapAction, SoapEnvelope envelope, CookieJar cj) throws IOException,
            XmlPullParserException {

         if (soapAction == null)
                soapAction = "\"\"";
            byte[] requestData = createRequestData(envelope);
            requestDump = debug ? new String(requestData) : null;
            responseDump = null;
            ServiceConnectionSE connection = getServiceConnection();
            connection.setRequestProperty("User-Agent", "kSOAP/2.0");
            connection.setRequestProperty("SOAPAction", soapAction);
            connection.setRequestProperty("Content-Type", "text/xml");
            connection.setRequestProperty("Connection", "close");
            connection.setRequestProperty("Content-Length", "" + requestData.length);
            connection.setRequestMethod("POST");

            Client client = new Client(); 

               if (cj != null)
                {
                    client.setCookies(connection.getConnection(), cj);

                }

            connection.connect();

            OutputStream os = connection.openOutputStream();

            os.write(requestData, 0, requestData.length);
            os.flush();
            os.close();
            requestData = null;
            InputStream is;
            try {
                connection.connect();
                is = connection.openInputStream();

                if(cj == null){
                    cj = client.getCookies(connection.getConnection());
                }
            } catch (IOException e) {
                is = connection.getErrorStream();
                if (is == null) {
                    connection.disconnect();
                    throw (e);
                }
            }
            if (debug) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[256];
                while (true) {
                    int rd = is.read(buf, 0, 256);
                    if (rd == -1)
                        break;
                    bos.write(buf, 0, rd);
                }
                bos.flush();
                buf = bos.toByteArray();
                responseDump = new String(buf);
                is.close();
                is = new ByteArrayInputStream(buf);
            }

            parseResponse(envelope, is);
            return cj;

    }

}

Original issue reported on code.google.com by edob...@gmail.com on 14 Jul 2010 at 6:00

GoogleCodeExporter commented 9 years ago
I'm getting an error in this line :

client.setCookies(connection.getConnection(), cj);

The error is :
The method getConnection() is undefined for the type ServiceConnectionSE

How can i solve this problem ?

Original comment by thoma...@gmail.com on 8 Oct 2010 at 8:20

GoogleCodeExporter commented 9 years ago
getConnection you should change source code add get mothod
my change source code see attachments

Original comment by edob...@gmail.com on 9 Oct 2010 at 12:45

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the replay, but i dont get it.
What shall i change ?

Can you add an exsample, please ?

Original comment by thoma...@gmail.com on 11 Oct 2010 at 1:01

GoogleCodeExporter commented 9 years ago
Please supply a patch against the latest version on github at mosabua or create 
clone and pull request.

Original comment by mosa...@gmail.com on 12 Oct 2010 at 5:33

GoogleCodeExporter commented 9 years ago

Original comment by mosa...@gmail.com on 12 Oct 2010 at 6:58

GoogleCodeExporter commented 9 years ago

Original comment by mosa...@gmail.com on 21 Oct 2010 at 4:47

GoogleCodeExporter commented 9 years ago
Markl is currently looking at implementing this for his needs and I will 
cooperate with him on it to get it pulled into the project code base and do 
another release when its done.
http://groups.google.com/group/ksoap2-android/browse_thread/thread/4eb910ef3f8b8
434

Original comment by mosa...@gmail.com on 5 Nov 2010 at 3:04

GoogleCodeExporter commented 9 years ago
I am in the process of implementing a cookie solution. Unfortunately the sonalb 
code is buggy and does not appear to be maintained anymore. Furthermore, it 
seems rather bloated for the requirements of this project (and I hate that 
Client class - why aren't those methods static? Is it that way so they could be 
overridden?) It was converted from a C++ project that was published in Dr. 
Dobbs but the original source for that does not seem to be available anymore. 
This leaves me believing that a more targeted rewrite would be a cleaner way to 
implement this functionality.

Original comment by markr...@gmail.com on 8 Nov 2010 at 4:12

GoogleCodeExporter commented 9 years ago
I assume you are talking about jcookie when you mention sonalb. I was not 
thinking of adding another library in.  I think it would be sufficient to 
expose the connection so that you can over ride the creation of the connection 
or at least have some sort of method that allows you to supply further header 
elements (including cookies) before the connection is established and in the 
same manner get out any header once the result was received. Then users can use 
jcookie or whatever else to set up their header but they dont have to and we 
wont impose that library on the many other users that have no need for cookies 
or further header stuff.. 

Makes sense?

Original comment by mosa...@gmail.com on 8 Nov 2010 at 4:48

GoogleCodeExporter commented 9 years ago
I see what you are getting at. I have a working prototype that I am currently 
testing. Part of the problem with the cookies is that there are three 
specifications all which vary slightly. I didn't want to write code to parse 
the cookies or the code to decide if a cookie should be sent to a particular 
web site. This seemed highly prone to error so I was looking for something 
already built. I discovered that there is a set of Apache classes already 
included in Android that seem to address that need. It has a cookie parser and 
a "match" function that will establish if the cookie is appropriate for the 
target domain and path. See org.apache.http.cookie as a starting point.
My implementation allows one to pass a "cookiejar" to the, for example, 
HttpTransportSE class, call method. This will then attach any relevant cookies 
to the request and add any received cookies to the jar. One only needs one jar 
since it will decide which cookies are relevant. It also handles replacing like 
named cookies. The sonalb code attempted something similar but was buggy.
To me, this seemed an intuitive approach. Thoughts?

Original comment by markr...@gmail.com on 15 Nov 2010 at 7:29

GoogleCodeExporter commented 9 years ago
Perfect. Makes sense to me. We just need to ensure that it is properly 
documented and maybe has an example on the wiki or somewhere and this should be 
good. 

Original comment by mosa...@gmail.com on 15 Nov 2010 at 8:38

GoogleCodeExporter commented 9 years ago
I have received a first cut of an implementation from markradb and we are 
looking at getting it into the project together.

Original comment by mosa...@gmail.com on 25 Nov 2010 at 5:58

GoogleCodeExporter commented 9 years ago
Issue 42 has been merged into this issue.

Original comment by mosa...@gmail.com on 7 Dec 2010 at 4:44

GoogleCodeExporter commented 9 years ago
Development on this feature is currently ongoing in 
https://github.com/markradb/ksoap2-android

Original comment by mosa...@gmail.com on 7 Dec 2010 at 4:46

GoogleCodeExporter commented 9 years ago
This has been pulled into the master branch. I am awaiting some content for a 
wiki page showing an example for usage and some testing feedback from markradb. 
Once that is available I will cut the new 2.5.3 release and close this issue.

Original comment by mosa...@gmail.com on 15 Dec 2010 at 6:31

GoogleCodeExporter commented 9 years ago
2.5.4 has been released and contains this fix. Documentation on the wiki would 
be great but the release is out for now.

Original comment by mosa...@gmail.com on 15 Feb 2011 at 6:16

GoogleCodeExporter commented 9 years ago

Original comment by mosa...@gmail.com on 15 Feb 2011 at 6:17

GoogleCodeExporter commented 9 years ago
Guys, just write code like this.. this will work

//part of the code 
  ServiceConnection connection = getServiceConnection();
        connection.setRequestProperty("User-Agent", "kSOAP/2.0");
    //    connection.setRequestProperty("User-Agent", "Jakarta Commons-HttpClient/3.1");
        connection.setRequestProperty("SOAPAction", soapAction);
        connection.setRequestProperty("Content-Type", "text/xml");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Length", "" + requestData.length);
        connection.setRequestProperty("Cookie","defaultinst_SAMLart=MDGsMcCYBoEEzZtIBRtVQulSMAfZVva5JH5cSizJPSbht7h9DAUmMmx7");
// use the above statement to set cookies.. this is working fine...

Original comment by rajesh.s...@gmail.com on 21 Feb 2011 at 11:25

GoogleCodeExporter commented 9 years ago
feel free to contact me for any questions reg this

Original comment by rajesh.s...@gmail.com on 21 Feb 2011 at 11:26

GoogleCodeExporter commented 9 years ago
It seems to be that there is a problem with multiple cookies.

The result of this code is setting only the last cookie =(

connection.setRequestProperty("Cookie","cookie1=foo");
connection.setRequestProperty("Cookie","cookie2=bar");

Any ideas, how to solve this problem?

Original comment by scratchboom on 21 Feb 2011 at 12:41

GoogleCodeExporter commented 9 years ago
My Apologies for a newbie question here, I am unable to find (built Jars of)

com.sonalb.net.http.cookie.Client;
com.sonalb.net.http.cookie.CookieJar;

would someone have them built that could provide here, if not please provide a 
understanding of how to export them from sources in Eclipse?

Original comment by acor...@gmail.com on 4 Apr 2011 at 9:10

GoogleCodeExporter commented 9 years ago
Why do you think you should use those and not javax.servlet.http ..

Original comment by mosa...@gmail.com on 4 Apr 2011 at 9:40

GoogleCodeExporter commented 9 years ago
Thank You, I would very much like to use javax.servlet.http would you have any 
example implementations with KSoap Sessions? I assume KSOAP2 is still used in 
that case. So you are saying use Java.servlet.http.HttpSession to manage 
session header Requests and Response still with KSoap2?

Original comment by acor...@gmail.com on 4 Apr 2011 at 10:16

GoogleCodeExporter commented 9 years ago
Sorry.. I have not implementation. Ask on the mailing list.

Original comment by mosa...@gmail.com on 4 Apr 2011 at 10:56