开发者

How to get response as XML Document with Apache Httpclient api?

I can receive the response. How can I get the response in a XML document? do I need to use an external XML parser? thanks for any helps

DefaultHttpClient client = new DefaultHttpClient();
String getUrl = "http://myurl.com";

HttpUriRequest getRequest = new HttpGet(getUrl);

getRequest.setHeader("User-Agent",  "xxxx");

 HttpResponse response = client.execute(getRequest);
 int statusCode = response.getStatusLine().getStatusCode();

 log.info("statusCode=" + statusCode);

 if (statusCode == 200 ){
  HttpEntity entity = response.getEntity();
  String content = EntityUtils.toString(enti开发者_JS百科ty);
  log.info("\n" + content);
 }else {
  log.warn("failed to response");
 }


I got my answer, post here for people have the same question

DefaultHttpClient client = new DefaultHttpClient();
String getUrl = "http://myurl.com";

HttpUriRequest getRequest = new HttpGet(getUrl);

getRequest.setHeader("User-Agent",  "xxxx");

 HttpResponse response = client.execute(getRequest);
 int statusCode = response.getStatusLine().getStatusCode();

 log.info("statusCode=" + statusCode);

Document doc = null;
        if (statusCode == 200 ){
            HttpEntity entity = response.getEntity();
            //String content = EntityUtils.toString(entity);

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                doc = builder.parse(entity.getContent());
            } catch (ParserConfigurationException e) {              
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }                           
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜