title: Android HTTP Post Request doesn't work, but stock Java does?
body: <p>So I have 2 codes which supposedly do the same. The one I'm using on Android however, returns the wrong HTML data. The stock Java one returns the correct data after sending the request. I have both codes here. Can you tell me (EVEN THOUGH I GAVE INTERNET PERMISSION TO ANDROID) why the Android one isn't working, while the stock Java one is working? This is the Android code: </p>
  
  <p>EDIT: I FOUND THE FIX. If you're going to use a StringEntity to send such a String to the Server, you have to set the content to application/x-www-form-urlencoded. I've edited my code to show this: </p>
  
  <pre><code>public static String sendNamePostRequest(String urlString) {
  
      HttpClient client = new DefaultHttpClient();
  
      HttpPost post = new HttpPost(urlString);
  
      StringBuffer sb = new StringBuffer();
  
      try {
             StringEntity se = new StringEntity(
                  "__EVENTTARGET=&amp;__EVENTARGUMENT=&amp;__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&amp;__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&amp;_nameTextBox=John&amp;_zoekButton=Zoek&amp;numberOfLettersField=3"); 
  
          se.setContent("application/x-www-form-urlencoded");
  
          post.setEntity();
  
          HttpResponse response = client.execute(post);
          HttpEntity entity = response.getEntity();
  
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  entity.getContent()));
          String in = "";
  
          while ((in = br.readLine()) != null) {
              sb.append(in + "\n");
          }
  
          br.close();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      return sb.toString();
  }
  </code></pre>
  
  <p>This is the stock Java code: </p>
  
  <pre><code>public String sendNamePostRequest(String urlString) {
  
      StringBuffer sb = null;
  
      try {
          String data = "__EVENTTARGET=&amp;__EVENTARGUMENT=&amp;__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&amp;__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&amp;_nameTextBox=John&amp;_zoekButton=Zoek&amp;numberOfLettersField=3";
  
          // String data = "";
  
          URL requestUrl = new URL(urlString);
  
          HttpURLConnection conn = (HttpURLConnection) requestUrl
                  .openConnection();
  
          conn.setDoOutput(true);
          conn.setDoInput(true);
          conn.setRequestMethod("POST");
  
          DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
  
          dos.writeBytes(data);
          dos.flush();
  
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  conn.getInputStream()));
  
          String in = "";
          sb = new StringBuffer();
  
          while ((in = br.readLine()) != null) {
              sb.append(in + "\n");
          }
  
          dos.close();
          br.close();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
      return sb.toString();
  }
  </code></pre>