Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Trying to send XML with HTTP POST to a Servlet

Thread view: 
g_asi2@yahoo.com - 16 Feb 2006 12:34 GMT
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
---------------------------------------------------------------

Thanks a lot!
Roedy Green - 16 Feb 2006 15:38 GMT
>                OutputStream out = connection.getOutputStream();
>                BufferedReader inStream = new BufferedReader(new
[quoted text clipped - 7 lines]
>                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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

g_asi2@yahoo.com - 19 Feb 2006 06:11 GMT
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

> >                OutputStream out = connection.getOutputStream();
> >                BufferedReader inStream = new BufferedReader(new
[quoted text clipped - 16 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.
Filip Larsen - 19 Feb 2006 11:07 GMT
> 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,
Signature

Filip Larsen



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.