Java:有没有办法更改收到的 HTTP 响应标头?
我正在使用 JAX-WS 生成的客户端(使用 wsimport,与 Glassfish 2.1.1 捆绑在一起的客户端)连接到在 IIS 6 中运行的 ASP.NET 生成的 WebService。
当我在响应中请求压缩(通过包含 HTTP 标头 Accept-Encoding: gzip 通过 JAX-WS SOAP 处理程序)时,IIS 6 使用压缩响应进行应答,但没有t 包括 Content-Encoding: gzip HTTP 响应标头,因此我收到以下异常:
com.sun.xml.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: XML reader error: com.sun.xml.stream.XMLStreamException2: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:361) at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:173)
at com.sun.xml.xwss.XWSSClientPipe.process(XWSSClientPipe.java:160)
at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:115)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)
at com.sun.xml.ws.client.Stub.process(Stub.java:248)
at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
编辑于 2011 年 4 月 17 日
我也尝试过,使用相同的 我用于请求的 SOAPHandler 压缩响应,修改 响应标头,但例外 发生在调用 Handler 之前。
结束编辑 2011 年 4 月 17 日
此外,当我使用“接受来自主机的压缩响应”首选项通过soapUI 3.6.1 向 WebService 发出相同的请求时,我可以看到我所说的内容: IIS 6 服务器不包括用于压缩的 HTTP 响应标头,soapUI 将响应显示为“二进制数据”并显示这些响应标头:
HTTP/1.1 200 OK
Date: Wed, 13 Apr 2011 08:50:55 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1104
如果 -withsoapUI- 我不请求压缩响应,我会得到下一个响应大小:
Content-Length: 2665
所以,正如我所说,这里的问题是 IIS6 没有在响应中添加 Contend-Encoding 标头。 我的问题是:是否可以以编程方式添加 Content-Encoding 标头?或者,也可能是:是否可以要求 IIS6 包含 Content-Encoding 标头?
更新
使用 Charles Web 调试代理 3.5.2 我已确认 IIS6 的响应不包含 Content-Encoding 标头:
HTTP/1.1 200 OK
Date Wed, 13 Apr 2011 10:51:53 GMT
Server Microsoft-IIS/6.0
X-Powered-By ASP.NET
X-AspNet-Version 2.0.50727
Cache-Control private, max-age=0
Content-Type text/xml; charset=utf-8
Content-Length 1110
我猜这可能是与 WebService 相关的问题,而不是与 IIS 相关的问题6
I'm using a JAX-WS generated client (using wsimport, the one bundled with Glassfish 2.1.1) to connect to a ASP.NET generated WebService running in a IIS 6.
When I request compression in the response (by including HTTP Header Accept-Encoding: gzip through JAX-WS SOAP Handlers) the IIS 6 answers with a compressed response, but doesn't includes the Content-Encoding: gzip HTTP response header, so i get the following Exception:
com.sun.xml.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: XML reader error: com.sun.xml.stream.XMLStreamException2: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:361) at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:173)
at com.sun.xml.xwss.XWSSClientPipe.process(XWSSClientPipe.java:160)
at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:115)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)
at com.sun.xml.ws.client.Stub.process(Stub.java:248)
at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
Edited Apr 17, 2011
I've also tried, using the same
SOAPHandler I use for requesting
compressed response, to modify the
Response Headers, but the Exception
occurs before the Handler is called.
End Edit Apr 17, 2011
Also, when I make the same request to the WebService through soapUI 3.6.1 with the Preference "Accept compressed responses from hosts", I can see what I've said: the IIS 6 server is not including the HTTP Response Header for compression, and soapUI shows the response as "binary data" and shows these response headers:
HTTP/1.1 200 OK
Date: Wed, 13 Apr 2011 08:50:55 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1104
If -with soapUI- I don't request compressed response I get the next response size:
Content-Length: 2665
So, the question here is, as I've said, that IIS6 is not adding the Contend-Encoding header in the response.
My question is: Is it possible to -programmatically- add the Content-Encoding header? Or, it also could be: Is it possible to ask IIS6 to include the Content-Encoding header?
UPDATE
Using Charles Web Debugging Proxy 3.5.2 I've confirmed the response from IIS6 doesn't include the Content-Encoding header:
HTTP/1.1 200 OK
Date Wed, 13 Apr 2011 10:51:53 GMT
Server Microsoft-IIS/6.0
X-Powered-By ASP.NET
X-AspNet-Version 2.0.50727
Cache-Control private, max-age=0
Content-Type text/xml; charset=utf-8
Content-Length 1110
I'm guessing this may be an issue more related to the WebService than to IIS 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上你需要两个组件。首先,您必须创建一个 过滤器 并将其添加到 web 中.xml 如下:
接下来,您可能需要创建 的包装器响应 其中您添加缺少的标头。
实际上,您可能根本不需要包装器,只需直接在 web.xml 中配置的过滤器中设置缺少的标头
希望这会有所帮助...
Basically you need two components. First you must create a filter and add it in web.xml as this:
Next you might need to create a wrapper of the response where you add the missing header.
Actually you might get not need the wrapper at all, just set the missing header directly in the filter you configured in web.xml
Hope this helps...
JAX-WS 特定的解决方法是尝试将处理程序放入客户端的输入链中。该处理程序将访问 servlet 上下文,获取请求对象,查看有效负载,如果它以 GZ 特定序列开头,则将 HTTP 标头添加到现有标头列表中。
缺点:可能不起作用。我希望 HTTP 标头列表是一个不可变的集合。此外,内容的解析(和失败)可能会早于第一个处理程序获得控制权。
(我在这里有另一个建议——使用 HTTP 过滤器——但后来我意识到 JAX-WS 是一个客户端,并且不使用 web.xml)。
JAX-WS-specific workaround is to try and place a handler into input chain of the client. The handler would access the servlet context, get the request object, look at the payload and if it starts with GZ-specific sequence, add the HTTP header to the list of existing ones.
Downsides: may not work. I expect the list of HTTP headers to be an immutable collection. Besides, the parsing of content (and the failure) may happen earlier than first handler will get the control.
(I had another suggestion here -- to use an HTTP Filter -- but then I realized JAX-WS is a client, and doesn't use web.xml).
你尝试过添加
到请求标头?
它与这个错误有关吗? (即使您不使用 IE,也可能会有所帮助)
Internet Explorer 失去使用 HTTP 压缩的 Web 服务器发回的前 2048 字节数据
have you tried adding
to request header?
is it associated to this bug? (eventhough ur not using IE, may be helpful)
Internet Explorer Loses the First 2048 Bytes of Data That Are Sent Back by Web Servers That Use HTTP Compression
我不太确定您的客户如何处理响应。但是,如果您可以以编程方式拦截 HttpResponse,则可以尝试类似于使用 servlet 过滤器来包装 HttpResponse 并扩充标头的操作。
I am not too sure how you're client handles the response. However if you can intercept the HttpResponse programmaticaly, you could try something similar to using a servlet filter to wrap the HttpResponse and augment the headers.