Netbeans Generated Java Web Service Client - Getting Http Status Code 307

I use Netbeans to create client-side web service code, client-style JAX-WS, so I can call the web service API.

However, when I call the web service API, I get an exception: com.sun.xml.internal.ws.client.ClientTransportException: the server sent an HTTP status code 307: temporary redirect

Why am I getting this? What is the workaround? I know that the problem is not with the web service itself, because I can get answers using soapUI and .Net.

+1
source share
2

.

- Apache CXF -, HTTP 307, .

- soapUI Follow Redirects, true, .

, , 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 ( , , didn Google) , - :

// 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);

, : BindingProvider.ENDPOINT_ACCESS_PROPERTY of retrieveMyObjectsPort, URL-, SOAP HTTP- POST, . , 307 - Temporary Redirect ( , 302 301), , URL-, -, .

checkRedirect -, :

  • URL-, http://example.com:50678/restOfUrl
  • - url https://example.com:43578/restOfUrl ( , -) - ​​ URL
  • -, .

: -, , - , , , , - .

,

0

, , , , , , , - . :

com.sun.xml.ws.client.ClientTransportException: The server sent HTTP status code 200: OK

. -, jax-ws , HTTP. , , .

'application/soap+xml' Content-Type.

0

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


All Articles