我通过 Java 发布到服务并收到不支持的内容编码:文本/xml 错误。
我知道该服务已成功接收并处理我的帖子并尝试发回适当的响应。该服务位于另一台计算机上,发出日志。我还将该日志发送给编写守护程序/服务的供应商,并被告知该服务符合我的要求,问题出在 Java 端。
根据错误消息判断,问题似乎是设置响应的 txt/xml 内容类型。
错误堆栈跟踪:
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:188)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at PkgNetAccelerator32.htsLink.createLink(htsLink.java:100)
...
Caused by: org.apache.http.HttpException: Unsupported Content-Coding: text/xml
at org.apache.http.client.protocol.ResponseContentEncoding.process(ResponseContentEncoding.java:98)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:139)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:200)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
... 14 more
源代码:
String strPathLocal = "http://192.168.20.65:8080/xml/link";
HttpPost httpPost = new HttpPost(strPathLocal);
String xmlPost = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Whatever>...</Whatever>";
StringEntity entity = new StringEntity(xmlPost);
entity.setContentType("text/xml");
httpPost.setEntity(entity);
CloseableHttpResponse response2 = httpclient.execute(httpPost);
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
EntityUtils.consume(entity2);
response2.close();
为了便于阅读,我去掉了 try-catch block 和某些其他内容。
代码永远不会超过 httpclient.execute() 行。
我做了一些搜索,看到一张票使用“application/xml”。我试过了,但这导致了同样的问题。
最佳答案
在我看来,您的服务器正在发送一个 header ,例如
Content-Encoding: text/xml
什么时候(可能)应该发送
Content-Type: text/xml
就目前而言,Apache 的HttpClient
可以处理gzip
、x-gzip
、deflate
类型的内容编码和 身份
。对于任何其他值,它都会抛出异常。
关于Java:不支持的内容编码:text/xml posting text/xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339637/