gpt4 book ai didi

xml - 如何使用 Apache Httpclient api 获取作为 XML 文档的响应?

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:52 27 4
gpt4 key购买 nike

我可以收到回复。如何获得 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");
}

最佳答案

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

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

关于xml - 如何使用 Apache Httpclient api 获取作为 XML 文档的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4216462/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com