Wrox Programmer Forums
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2005, 10:46 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default XML HTTP Post

Hi

I am new to using xml with HTTP posting and would like some advice. I need to read an xml file and post the xml data via http to a webserver.

The problem is the webserver returns that no data has been sent. What I have got is as follows:

1. A text file containing the xml data. I read from the text file as follows:

FileInputStream fis = new FileInputStream("c:/test.txt");
        int x= fis.available();
        byte b[]= new byte[x];
        fis.read(b);
        String xmldata = new String(b);

2. The I do the HTTP post as follows

String hostname = "wsisf29";
      int port = 7001;
      InetAddress addr = InetAddress.getByName(hostname);
      Socket sock = new Socket(addr, port);
        System.out.println("Connected");
      //Send header
      String path = "/aws/core/online";
      BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));

      System.out.println("Start Writing");
     wr.write("POST " + path + " HTTP/1.0\r\n");
     wr.write("Host: www.pascalbotte.be\r\n");
      wr.write("Content-Length: " + xmldata.length() + "\r\n");
      wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
      wr.write("\r\n");
        System.out.println(xmldata.length());
               // System.out.println(xmldata.);
      //Send data
      wr.write(xmldata);


      wr.flush();

If I do a sout on xml data it does return the whole set of xml text. However the server returns data sent invalid. Do i need to post the xml data line by line?

Any advice appreciated.

Regards
Marnus
 
Old May 21st, 2005, 03:40 AM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

If not already done, change the Host in your mime header.

Hope this help,

Pascal
 
Old June 2nd, 2005, 10:59 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have done sth similar.
Maybe it will help.
Code:
String data = "HERE IS XML FILE";
String path = "/servlet/xml";
osw.write("POST "+path+" HTTP/1.0\r\n");
osw.write("Content-Length: "+data.length()+"\r\n");
osw.write("Content-Type: application/x-www-form-urlencoded\r\n");
osw.write("Connection: keep-alive\r\n");
osw.write("\r\n");
osw.write(data);
osw.flush();
And it works fine.
BTW. for sure you dont need to POST xml file line by line.

M








Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP post silver_scar C# 2005 0 July 31st, 2006 09:52 AM
HTTP POST/GET d_alchemist JSP Basics 1 April 24th, 2006 09:18 AM
Sending XML via http post, using servlet kermitus Servlets 1 March 14th, 2006 07:03 PM
HTTP-POST with C# jcascio ASP.NET 1.0 and 1.1 Professional 3 April 12th, 2004 01:15 PM
Single HTTP POST - Multiple XML docs denniswong288 XML 1 August 26th, 2003 05:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.