简体   繁体   English

如何在调用Soap客户端时设置连接超时

[英]how to Set connection timeout while invoking soap client

Am new to webservice. 是Web服务的新手。 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. 但是,当托管Web服务的服务器没有响应时,我们将无法在位置“”的wsdl上访问wdl失败。连接超时:在25秒后打开流问题时进行连接。 During this 25 sec browser hangs up. 在这25秒内,浏览器挂起。 So i want to restrict connection timeout to 5 seconds. 所以我想将连接超时限制为5秒。 How can i set this to 5 seconds? 我如何将其设置为5秒? 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. 提前致谢。

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 对于org.apache.cxf,您可以尝试以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM