mmm that's strange. First of all, are you using a real device or the
emulator? Which android version? Are you connecting through Wi-Fi o Mobile
Network?

I just tried your code and it seems fast (max 6 seconds...but never 20).

For the PrintStream, you might try wrapping the OutputStream inside a
BufferedOutputStream, like this:
PrintStream os = new PrintStream (new BufferedOutputStream (urlConnection.
getOutputStream()));


What's odd is that by using this URLConnection, every request takes about 2
seconds...instead with the Apache HttpClient the first request would take
like 20 seconds, and the next ones less than 1 second...grrrrr
Also, with 3G it seems slightly slower (although now I'm really confused...I
did a couple of requests, and it took between 0.5 to 2 seconds each :S)..

Maybe someone has a bit more of experience and can explain what's happening?
Or what is the *correct* way of doing https requests (possibly fast :D)?


Thanks!
Yuvi


On Wed, Jan 27, 2010 at 1:18 PM, Ronan <[email protected]> wrote:

> Hi Yuvi,
>
> did you find that implementing the code using url.openConnection()
> improved the performance of the initial connection to SSL?
>
> i implemented as follows
>
> try {
>
>                        String myHttpsUrl = "
> https://myurl/api/find_user.xml";;
>                         URL url = new URL(myHttpsUrl);
>                         HttpsURLConnection urlConnection =
> (HttpsURLConnection)
> url.openConnection();
>                        urlConnection.setDoOutput(true);
>                        urlConnection.setDoInput(true);
>                        urlConnection.setFollowRedirects(false);
>                        Log.w("dial2do","getting output stream");
>                        PrintStream os = new PrintStream(
> urlConnection.getOutputStream());
>                        Log.w("dial2do","got output stream");
>                        StringBuilder sbOUT = new StringBuilder();
>                        sbOUT.append("value1=test2");
>                        sbOUT.append("&value2=test2");
>                        os.print(sbOUT.toString());
>                         BufferedReader br = new BufferedReader(new
> InputStreamReader
> (urlConnection.getInputStream()));
>                         String line;
>                        StringBuilder htmlContent = new StringBuilder();
>                        while ((line = br.readLine()) != null) {
>                                htmlContent.append(line);
>                        }
>                         Log.w("dial2do", htmlContent.toString());
>                } catch (Exception e) {
>                        e.printStackTrace();
>                }
>
> but i find that i get up to 20 sec delay on PrintStream os = new
> PrintStream( urlConnection.getOutputStream());
>
> has this been your experience?
>
> thanks in advance
>
> Ronan
>
> On Jan 26, 10:07 pm, Yuvi <[email protected]> wrote:
> > I think I found why this protocol error occurs. It seems the problem are
> > indeed the redirects, but only when there are redirects that go from
> HTTPS
> > to HTTP. In fact I tried also that same code with urlhttps://
> www.google.com,
> > and it gave the same error (it was redirecting tohttp://www.google.com).
> >
> > To disable the automatic redirect following I used:
> > HttpsURLConnection.setFollowRedirects(false);  // for HTTPS requests
> > HttpURLConnection.setFollowRedirects(false);  // for HTTP requests
> >
> > Now I have to find out how to read the http/s response headers from the
> > URLConnection, to get the "location" field. (if someone already knows
> > that.....telll meeee :D:D).
> >
> > Ciao!
> > Yuvi
> >
> > On Tue, Jan 26, 2010 at 9:52 PM, Jason Proctor <
> >
> >
> >
> > [email protected]> wrote:
> > > well this does seem to point to a long initialisation period for the
> Apache
> > > library. i don't have anything informed to suggest on the protocol
> error -
> > > maybe the Apache library supports more SSL protocols than the regular
> Java
> > > network library? dunno. checking versions etc couldn't hurt.
> >
> > > there's a bunch of redirects going on with
> thathttps://mip.sunrise.chURL, btw. something somewhere might be falling
> over that.
> >
> > > hth
> >
> > >  Yeah probably it has something to do with all the initialization
> stuff...I
> > >> just tried *not* using the Apache HttpClient, with the following code:
> >
> > >> String myHttpsUrl = "<https://mip.sunrise.ch/>https://mip.sunrise.ch
> ";
> >
> > >> URL url = new URL(myHttpsUrl);
> > >> URLConnection urlConnection = url.openConnection();
> >
> > >> BufferedReader br = new BufferedReader(new
> > >> InputStreamReader(ucon.getInputStream()));
> > >> String line;
> > >> StringBuilder htmlContent = new StringBuilder();
> > >> while ((line = br.readLine()) != null) {
> > >>    htmlContent.append(line);
> > >> }
> >
> > >> (I'm reading the whole content just to make sure my request has been
> > >> executed and fetched..).
> >
> > >> Anyway....it seems much faster!! (first request around 2 seconds,
> compared
> > >> to the HttpClient's 6, 10 or 20 seconds).
> >
> > >> However there is a problem: with the current https url it actually
> doesn't
> > >> work (it worked for other https addresses). I get back the following
> error
> > >> (with the url "<https://mip.sunrise.ch/>https://mip.sunrise.ch";):
> >
> > >> java.io.IOException: SSL handshake failure: Failure in SSL library,
> > >> usually a protocol error
> > >> error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
> > >> (external/openssl/ssl/s23_clnt.c:585 0xaf589f78:0x00000000)
> > >>    at
> > >>
> org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeconnect(Nativ
> e
> > >> Method)
> > >>    at
> > >>
> org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(Open
> SSLSocketImpl.java:308)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:173)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLCon...)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.jav...)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnecti...)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:...)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection....)
> > >>    at
> > >>
> org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnecti...)
> >
> > >> It seems like an ssl version error...mmmm....
> > >> Maybe I have to specify explicitly this version somewhere? Or maybe
> some
> > >> certificates? :S
> >
> > >> If anyone has an idea please let me know :)
> >
> > >> Thanks everyone!
> > >> Yuvi
> >
> > >> ....
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to