POST-ing data to Http server

J

Jonathan

Hi,
I'm trying to post (using POST method) form-like data (Let's say
parameter name = X; Value=ABC) from a Java program to a server, but it
doesn't seem to work. Any help would be greatly appreciated. This is
the code I'm using :

String data = "X=ABC";
URL u = new URL(url);
URLConnection connection = u.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + data.length());
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(connection.getOutputStream()));
out.write(data);
out.flush();
out.close();
connection.connect();

Thanks for your help!

Jonathan
 
G

Gregory A. Swarthout

Hi,
I'm trying to post (using POST method) form-like data (Let's say
parameter name = X; Value=ABC) from a Java program to a server, but it
doesn't seem to work. Any help would be greatly appreciated. This is
the code I'm using :

String data = "X=ABC";
URL u = new URL(url);
URLConnection connection = u.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + data.length());
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(connection.getOutputStream()));
out.write(data);
out.flush();
out.close();
connection.connect();

Thanks for your help!

Jonathan

String ENCODING = "UTF-8";


try {
URL url = new URL("http://www.website.com");
HttpURLConnection httpURLConnection =
(HttpURLConnection)url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
StringBuffer sbContent = new StringBuffer();
sbContent.append("X=");
sbContent.append(URLEncoder.encode("ABC", ENCODING));
DataOutputStream stream = new
DataOutputStream(httpURLConnection.getOutputStream());
stream.writeBytes(sbContent.toString());
stream.flush();
stream.close();
InputStream inputStream =
httpURLConnection.getInputStream();
inputStream.close();
} catch (Throwable t) {
}
 
J

Jonathan

String ENCODING = "UTF-8";


try {
URL url = new URL("http://www.website.com");
HttpURLConnection httpURLConnection =
(HttpURLConnection)url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
StringBuffer sbContent = new StringBuffer();
sbContent.append("X=");
sbContent.append(URLEncoder.encode("ABC", ENCODING));
DataOutputStream stream = new
DataOutputStream(httpURLConnection.getOutputStream());
stream.writeBytes(sbContent.toString());
stream.flush();
stream.close();
InputStream inputStream =
httpURLConnection.getInputStream();
inputStream.close();
} catch (Throwable t) {
}

I've tried your suggestion but no success. I'm trying to call a
servlet with my code. I can reach the servlet and debug. A call to
request.getParameter("X") is returning null... also, whent I check the
method in the request object, it's a GET!! Any help???
 
D

dtsas

I've tried your suggestion but no success. I'm trying to call a
servlet with my code. I can reach the servlet and debug. A call to
request.getParameter("X") is returning null... also, whent I check the
method in the request object, it's a GET!! Any help???


Sounds like the Servlet you are calling does not have the getPost()
method implemented? Can you check that? If the servlet does not have
the POST handling methods, then you will only be able to use the GET
handling. Sounds simple, but many people forget to implement the one
they need. Anyway, hope this helps.
 
B

Berlin Brown

Sounds like the Servlet you are calling does not have the getPost()
method implemented? Can you check that? If the servlet does not have
the POST handling methods, then you will only be able to use the GET
handling. Sounds simple, but many people forget to implement the one
they need. Anyway, hope this helps.

Another question, what happens when you leave out the POST, I have code
that works and for some reason gives me proper results, when I include
POST, the code is slower and doesnt connect properly, weird.
 
B

Berlin Brown

Sounds like the Servlet you are calling does not have the getPost()
method implemented? Can you check that? If the servlet does not have
the POST handling methods, then you will only be able to use the GET
handling. Sounds simple, but many people forget to implement the one
they need. Anyway, hope this helps.

I guess I will have to look at the java source again.
 
A

Andrew Thompson

"Berlin Brown" ...
(e-mail address removed) wrote: .... ...
I guess I will have to look at the java source again.

Above is an example of how you
_might_ have responded to that.

Note Berlin, that to put your short reply
after a very long message, with no trimming,
is actually the worst of all worlds.

Please trim irrelevant text in future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top