use this :
String responseFromServer = startUrlConnection("http://test.com/api/something.php", "username=testing&password=123");
-
private String startUrlConnection(String targetURL, String urlParameters) throws IOException {
URL url;
connection = null;
String output = null;
try {
// Create connection
url = new URL(targetURL);
System.setProperty("javax.net.debug", "all");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// Get Response
InputStream is;
if (connection.getResponseCode() <= 400) {
is = connection.getInputStream();
} else {
/* error from server */
is = connection.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
output = response.toString();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return output;
}