Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:58+00:00 2026-05-27T15:38:58+00:00

this is my first question, so please, bear with me. I have a Swing

  • 0

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) { ... }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:38:58+00:00Added an answer on May 27, 2026 at 3:38 pm

    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) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question, so please bear with me. I am working on
This is my first question here so please bear with me - I apologise
This is my first question asked here at stackoverflow, so bear with me please
Greetings everyone. This is my first question here at stackoverflow so please bear with
This is my first question so please be patient :) Background: I'm implementing an
this is my first question.. so, here we go. i have a site, 100%
Hello this is may first question and I have found so far the following
This will be quite a long way to ask my question, so please bear
This question is a bit long, please bear with me. In REST, i think
This is not a traditional scale-up or scale-out question. Please bear with me, here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.