[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Transfering XML Documents Using HTTP Post - Objects vs text <or> myenvironment or yours
- From: Jerry Murray <Jmurray@Ironplanet.com>
- To: xml-dev@lists.xml.org
- Date: Tue, 02 Jan 2001 20:07:57 -0800
I have an application where we need to accept XML documents from one company
(Microsoft based environment) our company (Unix / Java environment) to
another company and then pass documents back the other way. We have agreed
that we will both have servers capable of accepting HTTP Posts. An issue
has developed on whether the body of the request should include a parameter
name.
My understanding is that it is common to use parameters in the post body
(even if it is just one value):
an example from
http://www.w3.org/MarkUp/html-spec/html-spec_8.html
The user then conducts an HTTP POST transaction using the URI
`http://www.w3.org/sample'. The message body would be (ignore the line
break):
name=John+Doe&gender=male&family=5&city=kent&city=miami&
other=abc%0D%0Adef&nickname=J%26D
I have include sample code for sending the post and code for receiving the
request. Is this an unusual way of doing it? If we don't use a parameter
name, then it seems that I will have to use request.getReader(); and
then get the text from the BufferedReader.
Any suggestions?
Thanks in advance,
Jerry Murray
jmurray@ironplanet.com
925-225-8746
Code for the Client
import java.net.*;
import java.io.*;
import java.net.HttpURLConnection.*;
public class testpost {
public static void main(String[] args) throws Exception {
String inputString = "";
String input;
BufferedReader in = new BufferedReader(new FileReader(arg[0]));
while((input = in.readLine()) != null)
inputString = inputString + input;
in.close();
URL downeyjrURL = new URL("http://www.ourhost.com/servlet/INBOX");
HttpURLConnection c = (HttpURLConnection)(downeyjrURL.openConnection());
c.setDoOutput(true);
PrintWriter out = new PrintWriter(c.getOutputStream());
// Here's whether the parameter is set.
out.println("xmldoc=" + URLEncoder.encode(inputString));
out.close();
BufferedReader in2 = new BufferedReader(new
InputStreamReader(c.getInputStream()));
String inputLine;
while((inputLine = in2.readLine()) != null)
System.out.println(inputLine);
in2.close();
}
}
Code for the server:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class INBOX extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String xml = request.getParameter("xmldoc");
out.println(xml);
String fileName = "input.xml";
PrintWriter fileWriter = new PrintWriter (new BufferedWriter(new
FileWriter(fileName)));
fileWriter.write(xml);
fileWriter.flush();
fileWriter.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}