title: Android BouncyCastle (SpongyCastle) HTTPS POST request
body: <p>I am trying to login to my TLS1.2 server on an old Android device running Jellybean 4.1.2, with the BouncyCastle (SpongyCastle) library, but it doesn't work. That version of Android does not have TLS enabled by default, so I needed the 3rd party lib to use it.</p>
  
  <p>There are two problems.
  1. I get a HTTP 302 instead of a JSON response.
  2. I don't know how to send a JSON payload (for other endpoints I'll be using</p>
  
  <p>With the HTTP 302, I get the following response:</p>
  
  <pre><code>Result: HTTP/1.1 302 Found
  Server: Apache-Coyote/1.1
  Cache-Control: private
  Expires: Thu, 01 Jan 1970 00:00:00 UTC
  Set-Cookie: JSESSIONID=83C535625CDEF9DEC3D7890F1A9C86B0; Path=/; Secure; HttpOnly
  Location: https://www.google.com/login/auth                                                               Content-Length: 0
  Date: Wed, 14 Mar 2018 15:32:19 GMT
  Via: 1.1 google
  Set-Cookie: GCLB=CMfzgbfeh7bLpwE; path=/; HttpOnly
  Alt-Svc: clear
  Connection: close
  </code></pre>
  
  <p>So it seems its trying to redirect to some sort of Google login, which is weird.</p>
  
  <p>Also, with number 2 above, where I'm trying to send a payload, do I just add another <code>output.write(myJSONPayload);</code> or do I have to do something else?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>{
      java.security.SecureRandom secureRandom = new java.security.SecureRandom();
      Socket socket = new Socket("www.myserver.com", 443);
  
      TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream(), secureRandom);
      DefaultTlsClient client = new DefaultTlsClient() {
          public TlsAuthentication getAuthentication() throws IOException {
              TlsAuthentication auth = new TlsAuthentication() {
                  // Capture the server certificate information!
                  public void notifyServerCertificate(Certificate serverCertificate) throws IOException {
                  }
  
                  public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
                      return null;
                  }
              };
              return auth;
          }
      };
      protocol.connect(client);
  
      java.io.OutputStream output = protocol.getOutputStream();
      output.write("GET / HTTP/1.1\r\n".getBytes("UTF-8"));
  
      //Get auth with my own class to generate base 64 encoding
      output.write(("Authorization: " + BasicAuthentication.getAuthenticationHeader("myuser", "mypass")).getBytes());
  
      output.write("Host: www.myserver.com/logon\r\n".getBytes("UTF-8"));
      output.write("Connection: close\r\n".getBytes("UTF-8")); // So the server will close socket immediately.
      output.write("\r\n".getBytes("UTF-8")); // HTTP1.1 requirement: last line must be empty line.
      output.flush();
  
      java.io.InputStream input = protocol.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
      String line;
      StringBuilder sb = new StringBuilder();
      try {
          while ((line = reader.readLine()) != null) {
              Log.d(TAG, "--&gt; " + line);
              sb.append(line).append("\n");
          }
      } catch (TlsNoCloseNotifyException e) {
          Log.d(TAG, "End of stream");
      }
  
      String result = sb.toString();
      Log.d(TAG, "Result: " + result);
  }
  </code></pre>
  
  <p>Another question, do I have the correct HOST specified? Am I right to have the base URL in the <code>Socket</code>, and the full URL in the <code>OutputStream</code>?</p>