title: HttpUrlConnection getOutputStream throws IOException
body: <p>I try to make my application communicate with my php server using HttpUrlConnection. I wrote a class that works fine on my computer but when I execute it on my phone it doesn't work at all.</p>
  
  <p>Here is my code :</p>
  
  <pre><code>            String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&amp;password=" + URLEncoder.encode(password, "UTF-8") + "&amp;remember=1";
              connection = (HttpURLConnection) new URL(BASE_URL + "user/login/app").openConnection();
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
              connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
              connection.setDoInput(true);
              connection.setDoOutput(true);
              DataOutputStream out = new DataOutputStream(connection.getOutputStream());
              out.writeBytes(data);
              out.flush();
              out.close();
  </code></pre>
  
  <p>The line  DataOutputStream out = new DataOutputStream(connection.getOutputStream()); throws this exception : </p>
  
  <pre><code>E/Cloud   (10393): java.io.IOException
  E/Cloud   (10393):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:85)
  E/Cloud   (10393):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:194)
  E/Cloud   (10393):  at com.andryr.runningpal.data.cloud.Cloud.login(Cloud.java:139)
  E/Cloud   (10393):  at com.andryr.runningpal.data.SessionList$2.onLogin(SessionList.java:177)
  E/Cloud   (10393):  at com.andryr.runningpal.ui.dialog.LoginDialogFragment$2.onClick(LoginDialogFragment.java:56)
  E/Cloud   (10393):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
  E/Cloud   (10393):  at android.os.Handler.dispatchMessage(Handler.java:99)
  E/Cloud   (10393):  at android.os.Looper.loop(Looper.java:154)
  E/Cloud   (10393):  at android.app.ActivityThread.main(ActivityThread.java:4624)
  E/Cloud   (10393):  at java.lang.reflect.Method.invokeNative(Native Method)
  E/Cloud   (10393):  at java.lang.reflect.Method.invoke(Method.java:511)
  E/Cloud   (10393):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
  E/Cloud   (10393):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
  E/Cloud   (10393):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I tested this code on my computer and it doesn't throw any exception.
  My manifest :</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p>Thanks</p>