Hi ,

Below is the code snippet which I am using to post data to HTTPS url.
It is working fine with the emulator. But it is not working in Device.
Even it is not throwing any error/exception in the device I have check
by degugging. Can someone please help me out , As I am trying this
from so many days. I am unable to figure out the issue.


public Boolean postData(String data) throws IOException {
      Boolean response = false;
      TrustManager[] trustAllCerts = new TrustManager[] {new
X509TrustManager() {
        @Override
        public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
          return null;
        }

        @Override
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String
authType) {
        }

        @Override
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String
authType) {
        }
      }};

      // Install the all-trusting trust manager
      try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new
java.security.SecureRandom());
 
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      } catch (Exception e) {
      }


      HostnameVerifier hostnameVerifier =
 
org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;


      SchemeRegistry registry = new SchemeRegistry();
      registry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
      SSLSocketFactory socketFactory =
SSLSocketFactory.getSocketFactory();
      socketFactory.setHostnameVerifier((X509HostnameVerifier)
hostnameVerifier);
      registry.register(new Scheme("https", socketFactory, 443));

      // Set verifier
      StringBuilder postDataBuilder = new StringBuilder();
      postDataBuilder.append("data").append("=").append(data);
      byte[] postData = postDataBuilder.toString().getBytes("UTF-8");

      URL postURL = new URL(url);

      HttpURLConnection conn = (HttpURLConnection)
postURL.openConnection();
      conn.setDoOutput(true);
      conn.setUseCaches(false);
      HttpsURLConnection.setDefaultHostnameVerifier(new
CustomizedHostnameVerifier());
      conn.setRequestMethod("POST");
      conn.setConnectTimeout(30000);
      conn.setRequestProperty("Content-Type", "application/x-www-form-
urlencoded");
      conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));

      // ------------------
      OutputStream out = conn.getOutputStream();
      // --------------------------
      out.write(postData, 0, postData.length);
      out.flush();
      out.close();
      conn.disconnect();
      return response;
    }
  }
  private static class CustomizedHostnameVerifier implements
HostnameVerifier {
    @Override
    public boolean verify(String hostname, SSLSession session) {
      return true;
    }
  }

Thanks ,
Mahesh

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