Problems with Java HTTP Post request in Netscape Navigator

Wath is the right way to post a HTTP request from Java. I have code which uses URLConnection object and works under Internet Explorer, but does not works under Netscape Navigator
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What is the error it's giving in netscape
goroAuthor Commented:
There is no error in Netscape - i just post a request to a simple asp script and in Netscape the asp script does not recieve the parameters of the request( But it starts )
The way to do it is described in detail below. Is this how you are doing it...?

Passing arguments or data to a CGI script via the POST method is not immediately obvious to the Java programmer attempting to do this for the first time. By default, the URLConnection class uses the GET method, in which case you pass your arguments as part of the URL. To use the POST method, you have to call setDoOutput with an argument of true, get the connection's output stream, and write your POST data to the stream. After you close the stream, you can start reading from connection's input stream to obtain the output of the CGI script. The code listing shows how to do this. It takes a URL of a CGI script as an argument, connects to the URL, and reads the POST data from standard input until it encounters the end of file (you can use Ctrl-D). After you're done typing in the POST data, the example program fetches the output from the CGI script and prints it to standard output.


public final class Post {

  public static final void main(String args[]) {
    String input;
    URL url;
    URLConnection connection;
    InputStream result;
    BufferedReader reader;
    BufferedWriter writer;
    byte[] buffer = new byte[1024];
    int bytes;

    if(args.length != 1) {
      System.err.println("usage: Post ");

    try {
      url = new URL(args[0]);
    } catch(MalformedURLException e) {

    try {
      connection = url.openConnection();
      // This tells the connection to use the POST method
      writer =
      new BufferedWriter(
               new OutputStreamWriter(connection.getOutputStream()));

      // Read the POST data from standard input until EOF
      reader = new BufferedReader(new InputStreamReader(;
      while((input = reader.readLine()) != null) {
      writer.write(input, 0, input.length());

      // Now that we've written the POST data, we can fetch the contents
      // of the URL.
      result = connection.getInputStream();
      while((bytes = > -1)
      System.out.write(buffer, 0, bytes);
    } catch(IOException e) {

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Dear Friend,

Hello inorder to implement a POST method in Java you have to use URLConnection and get the outputstream from it and then send the data and then read it back. As an example

Suppose that you want to send data from a form to a CGI program called validate.cgi then

URL u=new URL("");
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(u.getOutputStream());
out.println(data to be writen);

BufferedReader in=new BufferdReader(new InputStreamReader(u.getInputStream());
do something with data

This method will work properly. You may require some changes.

Try it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goroAuthor Commented:
I tried this one also and I had the same problems, so I just moved to GET requests.

There is still no error on the client side?

Perhaps try encoding the parameters:

String param = URLEncoder.encode(stringToSend);

There is some more info here...

with another fully working example and the details of an example CGI script Sun have provided which you can use to test your code.

You could also try this more direct form of Post:
try {
  String server = getDocumentBase().getHost(), script = "cgi_name";
  int port = getDocumentBase().getPort();
  Socket s = null;

  try {
    s = new Socket(server, port);
    PrintWriter out = new PrintWriter(s.getOutputStream());
    BufferedReader is = new BufferedReader(new InputStreamReader(s.getInputStream()));
    out.print( "POST " + script + cgiParams + " HTTP/1.0\r\n" +
               "Content-type: application/x-www-form-urlencoded\r\n" +
               "Content-length: " + postData.length() +
               "\r\n\r\n" );


  StringBuffer buf = new StringBuffer();
  int input;
  while ((input = > -1)
    buf.append((char)input); is.close();

  return buf.toString();
} catch (Exception e) {

ALSO, goro:

Don't feel forced into accepting an answer just because someone post's it after the other comments provided...this is why people post comments so that you can choose which comment to accept as an answer. Not your fault I know but don't feel obliged to accept such answers above the comments previously provided - especially if they are wrong!

(Did you try the example I gave?)


I don't believe your answer is correct. Not only is it a copy of information given in previous comments but it misses out vital information required to work as you require. That is, You need to call setDoOutput on the connection to your script or else it will not POST the data you send down the stream.

Please don't post answers that just copy other comments...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.