Posted By:
Yasir_Khan
Posted On:
Tuesday, May 13, 2003 06:57 AM
I am writing here the code that connects to a PHP page and sends some prameters to the page as POST and then it prints the whole HTML response from PHP page to console, I hope following code will help:
try{
URL url = new URL("http://mywebsite:port/scripts/validate.php");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream os = urlConnection.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String postString = "Name"+"="+"Yasir Khan"+
"&Email"+"="+"yasir.khan@ascertia.com";
dos.writeBytes(postString);
InputStream is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line= null;
while( (line = br.readLine()) != null ){
System.out.println(line);
}
System.out.println("line "+line);
dos.close();
br.close();
urlConnection.disconnect();
}
catch(Exception ex) {
ex.printStackTrace();
}
Regards,
YK