Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JSP Interest »

Re: How to Use HTTP POST in standalone code

Chakravarthy, Sai

2006-05-10


You can use java.net.HttpURLConnection or
org.apache.commons.httpclient.HttpClient

for any HTTP methods. See the sample for HttpURLConnection.



URL url = new URL( "http", "localhost", 8800, "/TestServlet");

    URLConnection connection = url.openConnection();

    ((HttpURLConnection)connection).setRequestMethod( "POST");

    connection.setDoInput( true);

    connection.setDoOutput( true);

    OutputStreamWriter out = new OutputStreamWriter(

connection.getOutputStream());

    out.write( "MODE=SAMPLE&OUTPUT=HTML");

    out.write( "\r\n");

    out.flush();

    out.close();

   BufferedReader br = new BufferedReader( new InputStreamReader(

connection.getInputStream()));

    while ( br.ready()) {

     System.out.println( br.readLine());

    }





-     Chakravarthy









-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:JSP-INTEREST@(protected)]
Sent: Wednesday, May 10, 2006 2:21 AM
To: JSP-INTEREST@(protected)
Subject: How to Use HTTP POST in standalone code



I am able to get the Server Response from a standalone code by passing

parameters appended to a URL, in a way it can be said Get method.I am not

clear how to use Post for the same, cos The Url length has a limitation in

processing the request.Any inputs will be of great help.





Please find the code below using URL to send the request.



import java.io.* ;

import java.net.* ;

import java.security.* ;

import java.util.* ;



class test{



public static void main(String [] args){

String inputfilename="";

String strLine="";

String strVar="";

int counter, numberOfTokens = 0;

inputfilename=args[0];

 try

 {

FileReader inputFileReaderdump  = new FileReader(inputfilename);

BufferedReader inputFile  = new BufferedReader(inputFileReaderdump);

while ((strLine = inputFile.readLine()) != null)

{

StringTokenizer strTokLine = new StringTokenizer(strLine, ",");

numberOfTokens = strTokLine.countTokens();

      String

XXX_URL="http://darkwing.brocade.com:8080/DEVPGC/JSP/PGACMultiItemReport.js

p?BPN=BPN&itemNumbers="+strLine;

               URL url = new URL(XXX_URL) ;

              URLConnection urlc = url.openConnection() ;

               urlc.setDoOutput(true);

               urlc.setDoInput(true);

BufferedReader br = new BufferedReader(new InputStreamReader

(urlc.getInputStream())) ;

String responseXml = "" ;

String s = null ;

while ((s = br.readLine()) != null){

responseXml += s ;

}

System.out.println("[RESPONSE] " + responseXml) ;

}

}catch(Exception e){System.out.println(e.getMessage());

}



}



}



===========================================================================

To unsubscribe: mailto listserv@(protected)
JSP-INTEREST".

For digest: mailto listserv@(protected)
DIGEST".



Some relevant archives, FAQs and Forums on JSPs can be found at:



http://java.sun.com/products/jsp

http://archives.java.sun.com/jsp-interest.html

http://forums.java.sun.com

http://www.jspinsider.com


===========================================================================
To unsubscribe: mailto listserv@(protected)".
For digest: mailto listserv@(protected)".

Some relevant archives, FAQs and Forums on JSPs can be found at:

http://java.sun.com/products/jsp
http://archives.java.sun.com/jsp-interest.html
http://forums.java.sun.com
http://www.jspinsider.com
©2008 junlu.com - Jax Systems, LLC, U.S.A.