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