Java: Is there a way to change the received HTTP response headers?

I am using the JAX-WS-created client (using wsimport, the one associated with Glassfish 2.1.1) to connect to the created ASP.NET WebService running in IIS 6.
When I request compression in the response (by including the Accept-Encoding: gzip HTTP header through the JAX-WS SOAP Handlers), IIS 6 responds with a compressed response, but does not include the Content -Encoding: gzip HTTP response, 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 April 17, 2011

I also tried using the same SOAPHandler I use a compressed response to request to change the response Header, but an exception occurs before the handler is called.

End Edit April 17, 2011

In addition, when I make the same request in WebService through soapUI 3.6.1 using the “Accept Compressed Responses from Hosts” option, I can see what I said: IIS 6 server does not include 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 do not request a compressed response, I get the following response size:

 Content-Length: 2665 

So the question here, as I said, is that IIS6 does not add the Contend-Encoding header to the response. My question is: is it possible to - program - add a Content-Encoding header? Or it could also be: is it possible to request IIS6 to include the Content-Encoding header?

UPDATE
Using Charles Web Debugging Proxy 3.5.2 I confirmed that the response from IIS6 does not 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 assume this may be a problem more related to WebService than to IIS 6

+6
source share
4 answers

Basically you need two components. First you have to create a filter and add it to web.xml as follows:

 <filter> <filter-name>yourFilter</filter-name> <filter-class>yourFilterClassWhichAddsTheCorrectHeader</filter-class> </filter> <filter-mapping> <filter-name>yourFilter</filter-name> <url-pattern>theServletUrlMappedToJaxWS</url-pattern> </filter-mapping> 

You may then need to create a response wrapper in which you add the missing header.

In fact, you may not need a shell at all, just set the missing header directly to the filter that you configured in web.xml

Hope this helps ...

+1
source

The JAX-WS workaround is an attempt to put a handler in the client input chain. The handler will access the servlet context, get the request object, look at the payload, and if it starts with a specific GZ sequence, add the HTTP header to the list of existing ones.

Disadvantages: may not work. I expect the list of HTTP headers to be an immutable collection. In addition, content analysis (and failure) may occur before the first handler receives control.

(I had another suggestion here - use an HTTP filter, but then I realized that JAX-WS is a client and does not use web.xml).

0
source

Have you tried adding

Accept-Encoding: gzip, deflate

to request a header?

Is this related to this error? (while ur doesn't use IE, it may be useful)

Internet Explorer loses the first 2048 bytes of data sent by web servers using HTTP compression

0
source

I'm not too sure how you customer process the response. However, if you can intercept the HttpResponse program file, you can try something similar to using a servlet filter to wrap the HttpResponse and increase the headers.

 public class HttpReponseWrapper extends HttpReponse { HttpResponse reponse; public HttpResponseWrapper(HttpResponse response) { this.response = response; } public String getContentEncoding() { return "Content-Encoding=gzip"; } } 
0
source

Source: https://habr.com/ru/post/885800/


All Articles