13 กุมภาพันธ์ 2559

Java HTTP POST

java can HTTP POST to web server as the below example

Code:

import java.net.HttpURLConnection;
.
.
.
try
{

// create url object
URL url = new URL("http://piggy-thailand-trip.blogspot.com");

// create connection object
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);

// for POST or GET
connection.setRequestMethod("POST");

// set content-type
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// set cookie
connection.setRequestProperty("Cookie", "key=value");

// post message
String strRequest = "name=java_tutorial&value=java_example";

// send post message
BufferedOutputStream bfs = new BufferedOutputStream(connection.getOutputStream());
byte[] bdat = strRequest.getBytes();
bfs.write(bdat,0,bdat.length);
bfs.flush()

// read output
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
// print output
System.out.println(line);
}
}
catch(Exception ex)
{}




from: http://piggy-java.blogspot.com/2008/07/java-tutorial-java-http-post.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (1 ปีย้อนหลัง)