0
votes

Am new to webservice. I had written a soap client and it was working fine. But when the server hosting the web services is not responding we are getting a Failed to access wsdl at the location "" Got Connection timed out: connect while opening stream issue after 25 seconds. During this 25 sec browser hangs up. So i want to restrict connection timeout to 5 seconds. How can i set this to 5 seconds? Below is my code

URL url=null;
try {
url = new URL(serviceUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
return "ERROR";
}

Service service = new Service(url);//Exception is thrown here
ServiceSoap soap = service.getServiceSoap();

Thanks in advance.

1

1 Answers

0
votes

You can use:

    url = new URL(serviceURL); // serviceURL is like "http://.../some.asmx"
    String actionUrl = "http://.../SomeService";
    URLConnection connection = url.openConnection();
    connection.setConnectTimeout(5000); //5 sec in milliseconds
    connection.setDoOutput( true );
    connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
    connection.setRequestProperty("SOAPAction", actionUrl);

    String file = (String)request;

        OutputStream reqStream = null;
        try {
            reqStream = connection.getOutputStream();
        } catch (IOException e) {
            throw e;
        }
        try {
            reqStream.write(file.getBytes());
        } catch (IOException e) {
            throw e;
        }

        InputStream resStream = null;
        try {
            resStream = connection.getInputStream();
        } catch (IOException e) {
            throw e;
        }

For org.apache.cxf you can try below code

Service service = new Service(wsdl);
ServiceSoap port = service.getServiceSoap();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(5000);
policy.setReceiveTimeout(5000);