goro
asked on
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
What is the error it's giving in netscape
ASKER
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.
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(MalformedURLExceptio n e) {
e.printStackTrace();
return;
}
try {
connection = url.openConnection();
// This tells the connection to use the POST method
connection.setDoOutput(tru e);
writer =
new BufferedWriter(
new OutputStreamWriter(connect ion.getOut putStream( )));
// Read the POST data from standard input until EOF
reader = new BufferedReader(new InputStreamReader(System.i n));
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)
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:
return;
}
try {
url = new URL(args[0]);
} catch(MalformedURLExceptio
e.printStackTrace();
return;
}
try {
connection = url.openConnection();
// This tells the connection to use the POST method
connection.setDoOutput(tru
writer =
new BufferedWriter(
new OutputStreamWriter(connect
// Read the POST data from standard input until EOF
reader = new BufferedReader(new InputStreamReader(System.i
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)
if you need more help - post your code.
or check
http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
http://www.javaworld.com/javaworld/javatips/jw-javatip41.html
or check
http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
http://www.javaworld.com/javaworld/javatips/jw-javatip41.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried this one also and I had the same problems, so I just moved to GET requests.
goro
goro
There is still no error on the client side?
Perhaps try encoding the parameters:
String param = URLEncoder.encode(stringTo Send);
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.getOutputStr eam());
BufferedReader is = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
out.print( "POST " + script + cgiParams + " HTTP/1.0\r\n" +
"Content-type: application/x-www-form-url encoded\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...
Perhaps try encoding the parameters:
String param = URLEncoder.encode(stringTo
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(
int port = getDocumentBase().getPort(
Socket s = null;
try {
s = new Socket(server, port);
PrintWriter out = new PrintWriter(s.getOutputStr
BufferedReader is = new BufferedReader(new InputStreamReader(s.getInp
out.print( "POST " + script + cgiParams + " HTTP/1.0\r\n" +
"Content-type: application/x-www-form-url
"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...