Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache HttpResponse获取未格式化的字符串而不是XML_Java_Apache_Servlets_Ldap_Httpresponse - Fatal编程技术网

Java Apache HttpResponse获取未格式化的字符串而不是XML

Java Apache HttpResponse获取未格式化的字符串而不是XML,java,apache,servlets,ldap,httpresponse,Java,Apache,Servlets,Ldap,Httpresponse,我在servlet上有一些Java代码来执行LDAP查找: HttpClient client = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(theURL); HttpResponse getResponse = client.execute(getRequest); InputStream in = getResponse.getEntity().getContent(); String encoding = "UTF-

我在servlet上有一些Java代码来执行LDAP查找:

HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(theURL);
HttpResponse getResponse = client.execute(getRequest);
InputStream in = getResponse.getEntity().getContent();
String encoding = "UTF-8";
String body = IOUtils.toString(in, encoding);
在Javascript中实现时,响应是一个XML样式的字符串,沿着

<result>
<firstName>Bob</firstName>
<lastName>Smith</lastName>
<email>bobsmith@example.com</email>
</result>

这很容易解析。然而,在Java中,我得到BobSmithbobsmith@example.com,值之间没有分隔符。LDAP必须返回必要的信息,因为我通过JS实现获得了所需的信息;有什么方法可以从HttpResponse对象获得可解析的结果吗?

随请求发送一个accept标头。默认情况下,服务器可能发送纯文本

getRequest.setHeader(HttpHeaders.ACCEPT, "application/xml");
[或者如果这不起作用]

getRequest.setHeader(HttpHeaders.ACCEPT, "text/xml");