Note that if a request goes through once and the identical request
doesn't show up on the server when issued a second time it's likely
because somewhere along the way the response is being served out of a
cache.

On Jan 6, 6:52 am, Filiz Gökçe <sonsuzlu...@gmail.com> wrote:
> Hi,
> I try to connect the https connection.
>
> The below source is working but not stable.
>
> Sometimes same url+parameters results in a proper way, sometimes nothing
> comes as a response. We also looked at the logs of server, when results are
> null, there seems no requests from the server.
>
> I really couldn’t understand the reason of this kind of behavior. Method
> same, url is same, parameters are same but connection is one time successful
> and one time failure.
>
> I tested 1.6, 2.1 and 2.2 and nothing changed. Do you know anything about
> SSL connections in Android. Is there any bug? You are my last chance.
>
> *public* *static* String postURL(String url)
>
>       {
>
>             String result = "";
>
>             String url2 = "";
>
>             String param = "";
>
>             String[] *keys* = *null*;
>
>             url2 = url.substring(0, url.indexOf("?"));
>
>             param = url.substring(url.indexOf(url2) + url2.length() + 1); //
>
>             *try*
>
>             {
>
>                   System.*out*.println("SSL URL " + url2 + " & param: " +
> param);
>
>                   PrintWriter outWriter = *null*;
>
>                   BufferedReader inReader = *null*;
>
>                   HttpsURLConnection ucon = *null*;
>
>                   StringBuffer parameter = *new* StringBuffer();
>
>                   parameter.append(param);
>
>                   String operatorAddress = *new* String(url2);
>
>                   *try*
>
>                   {
>
>                         HostnameVerifier hv = *new* HostnameVerifier()
>
>                         {
>
>                             �...@override
>
>                              *public* *boolean* verify(String urlHostName,
> SSLSession session)
>
>                              {
>
>                                    System.*out*.println("Warning: URL Host:
> " + urlHostName + " vs. " + session.getPeerHost());
>
>                                    *return* *true*;
>
>                              }
>
>                         };
>
>                         // Create a trust manager that does not validate
> certificate
>
>                         // chains
>
>                         TrustManager[] trustAllCerts = *new* TrustManager[]
> { *new* X509TrustManager()
>
>                         {
>
>                              *public* java.security.cert.X509Certificate[]
> getAcceptedIssuers()
>
>                              {
>
>                                    *return* *null*;
>
>                              }
>
>                              *public*
> *void*checkClientTrusted(java.security.cert.X509Certificate[] certs,
> String
> authType)
>
>                              {
>
>                              }
>
>                              *public*
> *void*checkServerTrusted(java.security.cert.X509Certificate[] certs,
> String
> authType)
>
>                              {
>
>                              }
>
>                         } };
>
>                         *try*
>
>                         {
>
>                               SSLContext sc = SSLContext.*getInstance*("TLS"
> );
>
>                              sc.init(*null*, trustAllCerts,
> *new*java.security.SecureRandom());
>
>                               HttpsURLConnection.*setDefaultSSLSocketFactory
> *(sc.getSocketFactory());
>
>                              HttpsURLConnection.*setDefaultHostnameVerifier*
> (hv);
>
>                              URL urlc = *new* URL(operatorAddress); // set
> URL
>
>                              ucon = (HttpsURLConnection)
> urlc.openConnection(); // open
>
>                              ucon.setHostnameVerifier(hv);
>
>                         }
>
>                         *catch* (Exception e)
>
>                         {
>
>                              System.*out*.println("CONNECTION EXCEPTION");
>
>                              e.printStackTrace();
>
>                         }
>
>                         // *connetion*
>
>                         ucon.setDoOutput(*true*); // set output
>
>                         ucon.setDoInput(*true*); // set *intput*
>
>                         *try*
>
>                         {
>
>                              outWriter =
> *new*PrintWriter(ucon.getOutputStream()); //
> open
>
>                              // output
>
>                              // stream
>
>                              outWriter.print(parameter); // send data
>
>                              outWriter.close(); // close output stream
>
>                         }
>
>                         *catch* (Exception e)
>
>                         {
>
>                              System.*out*.println("OUTWRITER EXCEPTION");
>
>                              e.printStackTrace();
>
>                         }
>
>                         *try*
>
>                         {
>
>                              inReader = *new*
> BufferedReader(*new*InputStreamReader(ucon.getInputStream())); //
> open
>
>                              // input
>
>                              // stream
>
>                              StringBuffer resultBuffer = *new* StringBuffer(
> ""); // result
>
>                              *while* ((result = inReader.readLine()) != *
> null*)
>
>                                    resultBuffer.append(result);
>
>                              result = (resultBuffer.toString()).trim();
>
>                              inReader.close();
>
>                         }
>
>                         *catch* (Exception e)
>
>                         {
>
>                              System.*out*.println("INREADER EXCEPTION");
>
>                              e.printStackTrace();
>
>                         }
>
>                         System.*out*.println("RESULT ----" + result);
>
>                   }
>
>                   *catch* (Exception e)
>
>                   {
>
>                         *try*
>
>                         {
>
>                              *if* (inReader != *null*)
>
>                                    inReader.close();
>
>                              *if* (outWriter != *null*)
>
>                                    outWriter.close();
>
>                         }
>
>                         *catch* (IOException ex)
>
>                         {
>
>                              System.*out*.println("UTILITIES EXCEPTION 1");
>
>                              e.printStackTrace();
>
>                         }
>
>                   }
>
>             }
>
>             *catch* (Exception e)
>
>             {
>
>                   System.*out*.println("UTILITIES EXCEPTION 2");
>
>                   e.printStackTrace();
>
>             }
>
>             *return* result;
>
>       }
>
> Thank you so much,
>
> Filiz Gökçe

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

Reply via email to