5

this is my first question, so please, bear with me.

I have a Swing application, which gets data in a for of XML from a server through HttpURLConnection. Now I'm trying to create a constant request-respond connection with the server, to check if there are any updates for the application (as the checking has to be done regularly and often (every second or so)).

In some question's comment I read that it would be better to use Apache HttpClient instead of HttpURLConnection to maintain a live connection, but I can't find any good example how to go from my current code to the one with HttpClient. Specifically, what to use instead of HttpURLConnection.setRequestProperty() and HttpURLConnection.getOutputStream()?

Document request = new Document(xmlElement);
Document response = new Document();

String server = getServerURL(datasetName);
try {
  URL url = new URL(server);
  try {
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestProperty("Content-Type","application/xml; charset=ISO-8859-1");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("POST");
    OutputStream output = connection.getOutputStream();

    XMLOutputter serializer = new XMLOutputter();
    serializer.output(request, output);

    output.flush();
    output.close();

    InputStream input = connection.getInputStream();
    String tempString = ErrOut.printToString(input);

    SAXBuilder parser = new SAXBuilder();
    try {
      response = parser.build(new StringReader(tempString));
    }
    catch (JDOMException ex) { ... }
    input.close();
    connection.disconnect();
  }
  catch (IOException ex) { ... }
}
catch (MalformedURLException ex) { ... }

3 Answers 3

5

I think apache provides all the examples.. if you are using httpclient 4 you can refer to this URL http://hc.apache.org/httpcomponents-client-ga/examples.html

Additionally you may find this useful.. w.r.t setting the response type etc http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

4

Thank you Santosh and Raveesh Sharma for your answers. I ended up using StringEntity, and this is what I have now:

Document request = new Document(xmlElement);
Document response = new Document();

XMLOutputter xmlOutputter = new XMLOutputter();
String xml = xmlOutputter.outputString(request);

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(getServerURL(datasetName));
post.setHeader("Content-type", "application/xml; charset=ISO-8859-1");

try
{
  StringEntity se = new StringEntity(xml);
  se.setContentType("text/xml");
  post.setEntity(se);
}
catch (UnsupportedEncodingException e) { ... }

try
{
  HttpResponse httpResponse = client.execute(post);

  BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
  String line = "";
  String tempString = "";
  while ((line = rd.readLine()) != null)
  {
    tempString += line;
  }

  SAXBuilder parser = new SAXBuilder();
  try
  {
    response = parser.build(new StringReader(tempString));
  }
  catch (JDOMException ex) { ... }
}
catch (IOException ex) { ... }
3

Here is snippet you need (this replaces most of the things you have in try block) ,

try {
String postURL= server;
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(postURL);;
client.executeMethod(postMethod);
InputStream input = postMethod.getResponseBodyAsStream();   
//--your subsequent code here

EDIT: Here is the example of posting XML to a server Using Http-Client.

2
  • Thanks, but what about adding the actual request text with my XML? I tried adding StringEntity to the postMethod, with the XML as a string, but right now I'm only getting "HTTP/1.1 400 Bad request".
    – Rolandas
    Dec 16, 2011 at 14:39
  • @Rolandas, I have edited the answer with a link to URL. Please check it out.
    – Santosh
    Dec 16, 2011 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.