title: How to do an HTTPS POST from Android?
body: <p>I want to do a HTTPS post method to send some data from my android app to my  website.</p>
  
  <p>I used <code>HttpURLConnection</code> first and it's working fine with my HTTP URL. My production website is on HTTPS and I want to send the same POST using <code>HttpsURLConnection</code>. Can someone help me use the class properly?</p>
  
  <p>I found some source at <a href="http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html" rel="noreferrer">this link</a>:</p>
  
  <pre><code>KeyStore keyStore = ...;    
  TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");    
  tmf.init(keyStore);
  
  SSLContext context = SSLContext.getInstance("TLS");   
  context.init(null, tmf.getTrustManagers(), null);
  
  URL url = new URL("https://www.example.com/");   
  HttpsURLConnection urlConnection = (HttpsURLConnection)
  url.openConnection();   
  urlConnection.setSSLSocketFactory(context.getSocketFactory());   
  InputStream in = urlConnection.getInputStream();
  </code></pre>
  
  <p>What should be the value of  <code>KeyStore keyStore = ...;</code>?</p>
  
  <p>I tried sending the data using the same  <code>HttpURLConnection</code>, but I am seeing some POST data is missed or in error.</p>
  
  <p>I've tried the method from <a href="https://stackoverflow.com/questions/11218022/android-https-post-to-bitbucket-issue-tracker-returns-filenotfoundexception?rq=1">this question</a>. I am pasting my code below</p>
  
  <pre><code>String urlParameters="dateTime=" + URLEncoder.encode(dateTime,"UTF-8")+
      "&amp;mobileNum="+URLEncoder.encode(mobileNum,"UTF-8");
  
  URL url = new URL(myurl);
  HttpsURLConnection conn;
  conn=(HttpsURLConnection)url.openConnection();
  
  // Create the SSL connection
  SSLContext sc;
  sc = SSLContext.getInstance("TLS");
  sc.init(null, null, new java.security.SecureRandom());
  conn.setSSLSocketFactory(sc.getSocketFactory());
  conn.setConnectTimeout(HTTP_CONNECT_TIME_OUT);
  conn.setReadTimeout(HTTP_READ_TIME_OUT);
  
  //set the output to true, indicating you are outputting(uploading) POST data
  conn.setDoOutput(true);
  //once you set the output to true, you don't really need to set the request method to post, but I'm doing it anyway
  conn.setRequestMethod("POST");
  conn.setFixedLengthStreamingMode(urlParameters.getBytes().length);
  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
  PrintWriter out = new PrintWriter(conn.getOutputStream());
  out.print(urlParameters);
  out.close();
  
  InputStream is = conn.getInputStream();
  BufferedReader in = new BufferedReader(new InputStreamReader(is));
  String inputLine;
  while ((inputLine = in.readLine()) != null) {
    response += inputLine;            
  }                   
  </code></pre>
  
  <p>The error I am getting is below:</p>
  
  <pre><code>05-12 19:36:10.758: W/System.err(1123): java.io.FileNotFoundException: https://www.myurl.com/fms/test
  05-12 19:36:10.758: W/System.err(1123):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
  05-12 19:36:10.758: W/System.err(1123):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
  05-12 19:36:10.758: W/System.err(1123):     at .httpRequest(SMSToDBService.java:490)
  05-12 19:36:10.758: W/System.err(1123):     at com..access$0(SMSToDBService.java:424)
  05-12 19:36:10.758: W/System.err(1123):     at com.$ChildThread$1.handleMessage(SMSToDBService.java:182)
  05-12 19:36:10.758: W/System.err(1123):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-12 19:36:10.758: W/System.err(1123):     at android.os.Looper.loop(Looper.java:156)
  05-12 19:36:10.758: W/System.err(1123):     at com.$ChildThread.run(SMSToDBService.java:303)
  </code></pre>