2016-02-12 64 views
2

我將web服務客戶端從jdk1.6 jax-ws(v1?舊的 - 2005)遷移到jdk1.7 jax- ws 2.2.10。 舊的功能,但我有一個新的問題: 該服務(來自另一個社會,PHP服務器)迴應我:http 406錯誤,不能接受。無法修改SOAP WebService客戶端中的http accept頭 - JAX-WS 2.2.10 - JDK1.7

的accept頭我送的是:

Accept: text/xml, multipart/related 
Content-Type: text/xml; charset=utf-8 

與舊版本,我有:

Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Content-type: text/xml;charset="utf-8" 

於是,我就修改HTTP頭像這樣(在新處理器中我結合處理程序鏈):

在我的客戶:

javax.xml.ws.Binding binding = ((BindingProvider) ServiceXXX).getBinding(); 
    List<Handler> hchain = binding.getHandlerChain(); 
    if (hchain == null) {hchain = new ArrayList<Handler>();} 
    hchain.add(new HTTPUserAgentHandler()); 
    binding.setHandlerChain(hchain); 

在我的新的處理程序:

Map<String, List<String>> headers = (Map<String, List<String>>) context.get(MessageContext.HTTP_REQUEST_HEADERS); 
if (null == headers) {headers = new HashMap<String, List<String>>();} 
headers.put("Accept", Arrays.asList("text/xml", "multipart/related", "*/*")); 
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers); 

我的HTTP標頭現在是正確的,但在HttpTransportPipe類,它的覆蓋:

 reqHeaders.put("Content-Type", Collections.singletonList(ct.getContentType())); 
     if (ct.getAcceptHeader() != null) { 
      reqHeaders.put("Accept", Collections.singletonList(ct.getAcceptHeader())); 
     } 
     if (binding instanceof SOAPBinding) { 
      writeSOAPAction(reqHeaders, ct.getSOAPActionHeader()); 
     } 

服務器,顯然1.1,需要我送與"*/*" 的接受任何想法我怎麼能做到這一點?

回答

1

我想我找到了一種方法,它不是完美的解決方案。(如果有人有更好的..) 所以: 我./src/META-INF/services添加了一個名爲com.sun.xml.ws.api.pipe.TransportTubeFactory文件,其中有:

com.YYY.XXX.MonHttpTransportTubeFactory 

我加入這個類:

package com.YYY.XXX; 

import com.sun.xml.ws.api.pipe.ClientTubeAssemblerContext; 
import com.sun.xml.ws.api.pipe.TransportTubeFactory; 
import com.sun.xml.ws.api.pipe.Tube; 
import com.sun.xml.ws.transport.http.client.HttpTransportPipe; 

public class MonHttpTransportTubeFactory extends TransportTubeFactory{ 

    @Override 
    public Tube doCreate(ClientTubeAssemblerContext context) { 
     context.setCodec(new MonSOAPBindingCodec(context.getBinding().getFeatures())); 
     return new HttpTransportPipe(context.getCodec(), context.getBinding()); 
    } 
} 

這一個:(現有SOAPBidingCodec JAXWS裏SRC的複印件):

public class MonSOAPBindingCodec extends MonMimeCodec implements com.sun.xml.ws.api.pipe.SOAPBindingCodec { 
... 
    public MonSOAPBindingCodec(WSFeatureList features, StreamSOAPCodec xmlSoapCodec) { 
     super(getSoapVersion(features), features); 

     this.xmlSoapCodec = xmlSoapCodec; 
     xmlMimeType = xmlSoapCodec.getMimeType(); 

     String clientAcceptedContentTypes = xmlSoapCodec.getMimeType() + ", */*"; 

     WebServiceFeature fi = features.get(FastInfosetFeature.class); 
... 

因此,該服務調用我的HttpTransportTubeFactory幷包含我的自定義SOAP編解碼器。

這不是很漂亮,但它的作品..

相關問題