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
goroAsked:
Who is Participating?
 
muraliramConnect With a Mentor Commented:
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("www.yourserver.com/cgi-bin/validate.cgi");
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(u.getOutputStream());
out.println(data to be writen);

BufferedReader in=new BufferdReader(new InputStreamReader(u.getInputStream());
while((data=in.readLine())!=null)
{
do something with data
}

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

Try it.
0
 
sen_kumCommented:
What is the error it's giving in netscape
0
 
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 )
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JodCommented:
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.

import java.io.*;
import java.net.*;

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 ");
      return;
    }

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

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

      // Read the POST data from standard input until EOF
      reader = new BufferedReader(new InputStreamReader(System.in));
      while((input = reader.readLine()) != null) {
      writer.write(input, 0, input.length());
      writer.write("\r\n");
      }
      writer.flush();
      writer.close();

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

(From inquiry.com)
0
 
goroAuthor Commented:
I tried this one also and I had the same problems, so I just moved to GET requests.

goro
0
 
JodCommented:
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...

http://web2.java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html

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" );

    out.print(postData);
    out.flush();

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

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


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?)


muraliram:

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...
0
All Courses

From novice to tech pro — start learning today.