繁体   English   中英

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

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

是Web服务的新手。 我写了一个肥皂客户,它工作正常。 但是,当托管Web服务的服务器没有响应时,我们将无法在位置“”的wsdl上访问wdl失败。连接超时:在25秒后打开流问题时进行连接。 在这25秒内,浏览器挂起。 所以我想将连接超时限制为5秒。 我如何将其设置为5秒? 下面是我的代码

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

提前致谢。

您可以使用:

    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;
        }

对于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