Trying to send XML with HTTP POST to a Servlet

G

g_asi2

Hi,
I am trying to send some XML stuff through HTTP POST using a Java HTTP
client and a Servlet, and it doesn't seem to work!
Can someone PLEASE help me?

Here is my client code:
---------------------------------------------------------------
public class WavHttpClient {
public static void main(String[] args) {
try {
//connect
URI uri = new URI("http", null,
"//10.100.14.86:8080/WavTransfer/WavServlet", null, null);
URL url = uri.toURL();
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
System.out.println(url.toString());

//initialize the connection
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);

connection.setRequestProperty("Content-type","text/xml");
connection.setRequestProperty("Connection",
"Keep-Alive");

OutputStream out = connection.getOutputStream();
BufferedReader inStream = new BufferedReader(new
InputStreamReader(connection.getInputStream()));

String fileContent = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n<WAV>\n";
fileContent = fileContent + "this is the
content</WAV>";

System.out.println("prepering to send ");
out.write(fileContent.getBytes());
out.flush();
connection.connect();
out.close();

String str;
while ((str = inStream.readLine()) != null)
System.out.println("Server: "+str);

connection.disconnect();
} catch (Exception e) {
System.out.println("Got Exception: " + e);
}

System.out.println("DONE");
}
}
---------------------------------------------------------------

Here is my server code:
---------------------------------------------------------------
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
BufferedReader reader = new BufferedReader(new
InputStreamReader(request.getInputStream()));
ServletOutputStream outStream =
response.getOutputStream();
outStream.println("OK, got you");
String message = null;
boolean gotNessage = false;
while ((message = reader.readLine()) != null) {
outStream.println("Read "+message);
gotNessage = true;
}
if (!gotNessage)
outStream.println("Got no message");
outStream.println("getContentLength: " +
request.getContentLength());
outStream.println("getContentType: " +
request.getContentType());
}
---------------------------------------------------------------

When I run the client I get the following output:
---------------------------------------------------------------
http://10.100.14.86:8080/WavTransfer/WavServlet
prepering to send
Server: OK, got you
Server: Got no message
Server: getContentLength: 0
Server: getContentType: text/xml
DONE
 
R

Roedy Green

OutputStream out = connection.getOutputStream();
BufferedReader inStream = new BufferedReader(new
InputStreamReader(connection.getInputStream()));

String fileContent = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n<WAV>\n";
fileContent = fileContent + "this is the
content</WAV>";

System.out.println("prepering to send ");
out.write(fileContent.getBytes());

It would be simpler to use a PrintWriter wrapping your OutputStream,
one with an EXPLICIT UTF-8 encoding.

see http://mindprod.com/applets/fileio.html
for sample code.
 
G

g_asi2

Hi,
Thank for the reply.
But the problem is not the simplicity (I got to that code after many
changes).
The problem is that even that I have a connection (the client get the
server's answer), the server is not able to read the client stream.
ANYONE??????
Asaf
 
F

Filip Larsen

I am trying to send some XML stuff through HTTP POST using a Java HTTP
client and a Servlet, and it doesn't seem to work!

Note that HttpConnection (as noted in the documentation for its methods)
will send the request to the server the first time you fetch any
result-like data from it, like when opening the input stream. In your
case it means that the input stream open should occur only after all
data has been flushed to the output stream and all request parameters
has been set.


Regards,
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top