I’m looking at the documentation here: http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html
I’m not seeing an option to flag a request as a POST method. I’d expect to see .setRequestMethod("POST"); or something. Am I looking at the wrong documentation?
I’m using Android 4.2 and Java 1.6. Though I’m unsure if javase/1.4.2 is what I should be looking for.
URL url = new URL(this.getUrl());
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
You need to use HTTPURLConnection instead of URLConnection.
HTTPURLConnection has setRequestMethod
Example:
Note: It is always better to use latest javadoc instead of old version, 1.4 is considerably older version of java.
Credit goes to Luigi R. Viggiano:
My answer above just deals with how you can access
setRequestMethod. If you don’t want to use setRequestMethod, but achieve POST, as Luigi suggested, you can ignore callingsetRequestMethodand just setsetDoOutput(true). Read this tutorial for more information.