Problem with sending a POST request

D

Drask

What I want to do is send an HTTP request from a java application to a
servlet (which is currently on the same computer).

Actualy, it works and I get an answer from the server.

My problem is.. it won't pass any POST value. If I pass my data using
GET method, it works, but when I try to do it in POST, when I test the
method type on my servlet, I find out that the method is still GET, and
I can't recover my request.

I really need to use POST method because the GET method seems to have
some limitations (size).

I tried something like 6 or 7 different ways to do it (all approved by
everyone arround the net). Here's one of them:

// Construct data
String request = "<request><requestid>" + item.ItemID +
"</requestid><type>" + item.Type + "</type><datestamp>" +
item.DateStamp + "</datestamp><message>" + item.Message +
"</message><description>" + item.Description +
"</description></request>";

String data = "request=" + URLEncoder.encode(request, "UTF-8");

// Send data
URL url = new URL(AMIMain.appconf.ServerURL);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

byte[] bytes = data.getBytes();

// Indicate that you will be doing input and output,
// that the method is POST, and that the content
// length is the length of the byte array

conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-length",
String.valueOf(bytes.length));

// Write the parameters to the URL output stream
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.flush();

// Read the response
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;

while ((line = in.readLine()) != null)
{
response = response + line;
}

// Close everything
in.close();
out.close();
conn.disconnect();

----

I tried everything. Tried sending an object and retrieving it (it still
can't retrieve it because it still thinks that it's dealing with a GET.
Tried differents ways to send the data through the stream of the
connection, even tried to send the data through a file.

Here's the code on the servlet:

System.out.println(request.getMethod()); // this is always GET, but it
should be POST
String s = request.getParameter("request");

----

I know that the servlet is OK because I tried to send the request using
a simple html file and it works (POST).

I just don't know what to think about it.. anyone have an idea?
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top