Netbeans中生成的Java Web Service客户端-获取Http状态码307

|| 我使用Netbeans生成Web服务客户端代码,客户端样式的JAX-WS,因此可以调用Web服务API。 但是,当我调用Web服务API时,出现异常: com.sun.xml.internal.ws.client.ClientTransportException:服务器发送了HTTP状态码307:临时重定向 为什么我得到这个?解决方法是什么?我知道问题不在于Web服务本身,因为我可以通过soapUI和.Net很好地获得响应。     
已邀请:
大约一个月前面临同样的问题。 使用Apache CXF生成Web服务客户端类,并且Web服务返回HTTP 状态307,导致相同的异常。 使用soapUI将属性“ 0”设置为“ 1”的相同的Web服务方法调用成功,并返回了所需的数据。 经过一段时间的搜索之后,JAX-WS中似乎没有用于启用以下重定向的属性。 因此,下面是当前正在运行的代码,尽管我不确定它是否符合任何标准: 假设生成的客户端类如下所示:
// generated service class
public class MyWebServiceClient extends javax.xml.ws.Service {
    // ...
    private final QName portName = \"...\";
    // ...
    public RetrieveMyObjects getRetrieveMyObjects() {
        return super.getPort(portName, RetrieveMyObject.class);
    }
    // ...
}

// generated port interface
// annotations here
public interface RetrieveMyObjects {

    // annotations here
    List<MyObject> getAll();

}
现在,在执行以下代码后:
MyWebServiceClient wsClient = new MyWebServiceClient(\"wsdl/location/url/here.wsdl\");
RetrieveMyObjectsPort retrieveMyObjectsPort = wsClient.getRetrieveMyObjects();
wsClient
应返回实例,该实例同时是
RetrieveMyObjects
javax.xml.ws.BindingProvider
接口的实例。在JAX-WS的表面上没有任何声明,但是似乎很多代码都基于该事实。通过执行以下操作,可以使他/她自己放心:
if(!(retrieveMyObjectsPort instanceof javax.xml.ws.BindingProvider)) {
    throw new RuntimeException(\"retrieveMyObjectsPort is not instance of \" + BindingProvider.class + \". Redirect following as well as authentication is not possible\");
}
现在,当我们确定
retrieveMyObjectsPort
javax.xml.ws.BindingProvider
的实例时,我们可以向其发送简单的HTTP POST请求,以模拟SOAP请求(尽管它看起来非常不正确和丑陋,但这在我的情况下是可行的,而在此期间我没有发现任何更好的选择)谷歌搜索),然后检查Web服务是否将发送重定向状态作为响应:
// defined somewhere before
private static void checkRedirect(final Logger logger, final BindingProvider bindingProvider) {
    try {
        final URL url = new URL((String) bindingProvider.getRequestContext().get(ENDPOINT_ADDRESS_PROPERTY));
        logger.trace(\"Checking WS redirect: sending plain POST request to {}\", url);
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod(\"POST\");
        connection.setRequestProperty(\"Content-Type\", \"text/html; charset=\'UTF-8\'\");
        connection.setDoOutput(true);

        if(connection.getResponseCode() == 307) {
            final String redirectToUrl = connection.getHeaderField(\"location\");
            logger.trace(\"Checking WS redirect: setting new endpoint url, plain POST request was redirected with status {} to {}\", connection.getResponseCode(), redirectToUrl);
            bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, redirectToUrl);
        }
   } catch(final Exception e) {
       logger.warn(\"Checking WS redirect: failed\", e);
   }
}

// somewhere at the application start
checkRedirect(logger, (BindingProvider) retrieveMyObjectsPort);
现在,此方法的作用是:占用ѭ8的
BindingProvider.ENDPOINT_ACCESS_PROPERTY
,即此端口方法将向其发送SOAP请求的URL,并如上所述发送普通的HTTP POST请求。然后,它检查响应状态是否为“ 13”(也可能包括其他状态,如302或301),如果是,则获取Web服务重定向到的URL并为指定的端口设置新的端点。 在我的情况下,每个Web服务端口接口都会调用一次此
checkRedirect
方法,然后一切似乎都可以正常工作: 重定向是在网址上检查的,例如
http://example.com:50678/restOfUrl
Web服务重定向到ѭ16之类的网址(请注意,存在Web服务客户端身份验证)-端口的端点设置为该网址 通过该端口执行的下一个Web服务请求成功 免责声明:我对Web服务非常陌生,由于缺少针对此问题的解决方案,因此我设法实现了这一目标,因此,如果此处出现问题,请更正我。 希望这可以帮助     
是的,我知道这篇文章很旧,但是我也遇到过类似的错误,并认为也许有人会从我的解决方案中受益。 最困扰我的是:
com.sun.xml.ws.client.ClientTransportException: The server sent HTTP status code 200: OK
原来这意味着响应头不完整。显然,jax-ws进行了某种验证,其中包括也验证HTTP标头。我正在使用的服务器只是发送一个空头。 将
\'application/soap+xml\'
添加到
Content-Type
标头后,它的工作原理就像一种魅力。     

要回复问题请先登录注册