如何使用 Apache Httpclient api 获取 XML 文档响应?

发布于 2024-10-03 08:11:19 字数 612 浏览 4 评论 0原文

我可以收到回复。如何获取 XML 文档中的响应?我需要使用外部 XML 解析器吗?感谢您的帮助

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(entity);
  log.info("\n" + content);
 }else {
  log.warn("failed to response");
 }

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(entity);
  log.info("\n" + content);
 }else {
  log.warn("failed to response");
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2024-10-10 08:11:20

我得到了答案,在这里发帖给有同样问题的人

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();
            }                           
        }

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();
            }                           
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文