RSS

Tag Archives: example

How do I send a POST request using Java?

// Create query string

String queryString = “param1=” +
URLEncoder.encode(param1Value, “UTF-8”);
queryString += “&param2=” +
URLEncoder.encode(param2Value, “UTF-8”);

// Make connection

URL url = new URL(“http://www.objects.com.au/”);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
urlConnection.getOutputStream());

// Write query string to request body

out.write(queryString);
out.flush();

// Read the response

BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line = null;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
out.close();
in.close();

 
Để lại phản hồi

Posted by on Tháng Hai 8, 2012 in Uncategorized

 

Nhãn: