java jax-ws:设置请求的内容类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jax-ws: setting Content-Type of request
提问by El Che
I've been trying to consume a .net WCF rest service using JAX-WS lately. Due to security of the service, I've have to set a custom HTTP header Authorization with some signature in it. This I've solved by doing something like this:
我最近一直在尝试使用 JAX-WS 来使用 .net WCF 休息服务。由于服务的安全性,我必须设置一个带有一些签名的自定义 HTTP 标头授权。我通过做这样的事情解决了这个问题:
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,
Collections.singletonMap("Authorization",Collections.singletonList(authHeader)));
However, I get an exception when invoking the service:
但是,在调用服务时出现异常:
com.sun.xml.internal.ws.server.UnsupportedMediaException: Unsupported Content-Type: text/html; charset=UTF-8 Supported ones are: [text/xml]
com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:284)
com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:118)
com.sun.xml.internal.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:278)
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:180)
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:83)
com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:105)
And it seems to me that there might be a requirement for my request in the service to be text/xml content-type. How do I set this? I've googled a lot, and read some documentation but I can't seem to figure it out. The exception could of course also be something else than the Content-Type setting (since I've read that by default jax-ws uses text/xml) but I'm not sure.
在我看来,我在服务中的请求可能需要为 text/xml 内容类型。我该如何设置?我在谷歌上搜索了很多,并阅读了一些文档,但我似乎无法弄清楚。例外当然也可能是 Content-Type 设置以外的其他东西(因为我已经读过默认情况下 jax-ws 使用 text/xml),但我不确定。
Thanks in advance!
提前致谢!
回答by Shiraz Bhaiji
You are getting a text/html content type back as a response.
您将获得 text/html 内容类型作为响应。
Most of the time when I have got this error, it has been that the server is sending an html error page.
大多数情况下,当我收到此错误时,是因为服务器正在发送一个 html 错误页面。
If you have a way to log the response, you should be able to see exactly what the problem is.
如果您有办法记录响应,您应该能够准确地看到问题所在。

![无法使用 java 将 byte[] 插入 MySQL](/res/img/loading.gif)