title: Using httpUrlConnection: am i setting up my connection correctly?
body: <p>I am making use of the <code>HttURLconnection</code> and <code>URLConnection</code> api's to connect to a <code>PHP</code> file on my web host (x10host). However the connection does not seem to be established and no string data is sent. </p>
  
  <p>I am sure that my <code>PHP</code> code is correct, so am I using the classes incorrectly in the below code?</p>
  
  <pre><code>@Override
              protected String doInBackground(String... params) {
  
                  StringBuilder respData = new StringBuilder();
                  InputStream stream = null;
                  OutputStream os = null;
                  HttpURLConnection httpUrlConnection;
                  URLConnection conn;
                  URL url;
  
                  try {
  
                      url = new URL("my_url/recieveString.php");
                      conn = url.openConnection();
                      httpUrlConnection = (HttpURLConnection) conn;
  
                      httpUrlConnection.setUseCaches(false);
                      //httpUrlConnection.setRequestProperty("User-Agent", "App");
                      httpUrlConnection.setConnectTimeout(30000);
                      httpUrlConnection.setReadTimeout(30000);
                      httpUrlConnection.setRequestMethod("POST");
                      httpUrlConnection.setDoOutput(true);
                      os = httpUrlConnection.getOutputStream();
  
                      toSubmit = "test";
  
                      stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
  
                      copy(stream, os);
  
                      httpUrlConnection.connect();
  
                      int responseCode = httpUrlConnection.getResponseCode();
  
                      if (200 == responseCode) {
                          InputStream is = httpUrlConnection.getInputStream();
                          InputStreamReader isr = null;
                          try {
                              isr = new InputStreamReader(is);
                              char[] buffer = new char[1024];
                              int len;
                              while ((len = isr.read(buffer)) != -1) {
                                  respData.append(buffer, 0, len);
                              }
                          } finally {
                              if (isr != null) {
                                  isr.close();
                                  success = true;
                              }
                          }
                          is.close();
                      } else {
                          // use below to get error stream
                          //inputStream = httpUrlConnection.getErrorStream();
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                      try {
                          stream.close();
                          os.flush();
  
                          os.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
  
                      return "done";
                  }
              }
  </code></pre>