title: PHP receiving a POST request from Java
body: <p>EDIT: this is what is shown in my website logs
  xx.xx.xxx.xx - - [27/Jan/2012:17:42:24 -0500] "POST /dir/addData2.php HTTP/1.1" 200 - www.mywebsites.com "-" "Java/1.7.0" "-"</p>
  
  <p>I am hosting my website at 1&amp;1, and I want to have page <code>blank.php</code> that should take a POST request and upload it to my database. I think I am sending my POST properly, and that somehow I am not handling it properly on my website. Because nothing is being inserted to my database.  The response has content length 0, but even if i send a header with the length of the string it wont change. Another option is that the host wont allow me to do remote post requests (still waiting on reply). </p>
  
  <p>I send the post request from a Java application like this:</p>
  
  <pre><code>URL url = new URL("www.mywebsite.com/blank.php");    
  HttpURLConnection request = (HttpURLConnection)url.openConnection();
  request.setRequestProperty("Content-type","application/x-www-form-urlencoded");
  request.setRequestMethod("POST");
  OutputStreamWriter post = new OutputStreamWriter(request.getOutputStream());
  String data = URLEncoder.encode("account", "UTF-8") + "=" + URLEncoder.encode(message[0], "UTF-8");
  data += "&amp;" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode(message[2], "UTF-8");
  data += "&amp;" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(message[0], "UTF-8");
  post.write(data);
  post.flush();
  /*
  / String example
  /account=103&amp;message=Feller+1391.88+0&amp;type=103
  */
  </code></pre>
  
  <p>The response from the server is:</p>
  
  <pre><code>null=[HTTP/1.1 200 OK]
  Date=[Fri, 27 Jan 2012 21:59:10 GMT]
  Content-Length=[0]
  Keep-Alive=[timeout=2, max=200]
  Connection=[Keep-Alive]
  Content-Type=[text/html]
  Server=[Apache]
  X-Powered-By=[PHP/5.2.17]
  </code></pre>
  
  <p>My webpage has this basic PHP code (right now, will improve/check for stuff later)</p>
  
  <pre><code>$link = mysql_connect($hostname, $username, $password);
  $db_selected = mysql_select_db($database, $link);
  $query = "INSERT INTO newData(account, message, type) VALUES('$_POST[account]', '$_POST[message]', '$_POST[type]')";
  mysql_query($query) || die();
  </code></pre>
  
  <p>I want to point out that I do not have a private server, and I will probably use <a href="http://hc.apache.org/httpcomponents-core-ga/examples.html" rel="nofollow">http://hc.apache.org/httpcomponents-core-ga/examples.html</a> later. Right now I just want to send a string from my Java application, receive it with PHP and insert to MySQL database. </p>