如何从HttpURLConnection切换到HttpClient [英] How to switch from HttpURLConnection to HttpClient

查看:205
本文介绍了如何从HttpURLConnection切换到HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,所以请耐心等待。

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

我有一个Swing应用程序,它通过HttpURLConnection从服务器获取XML格式的数据。现在我正在尝试与服务器创建一个持续的请求 - 响应连接,以检查应用程序是否有任何更新(因为检查必须定期和经常(每隔一秒左右))。

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)).

在一些问题的评论中,我读到最好使用Apache HttpClient而不是HttpURLConnection来维持实时连接,但我找不到任何好的例子如何从我的当前使用HttpClient编写代码。具体来说,使用什么而不是HttpURLConnection.setRequestProperty()和HttpURLConnection.getOutputStream()?

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) { ... }


推荐答案

感谢Santosh和Raveesh Sharma的回答。
我最终使用了StringEntity,这就是我现在所拥有的:

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) { ... }

这篇关于如何从HttpURLConnection切换到HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆