gpt4 book ai didi

java - 在 JAX-WS web 服务调用中捕获 ConnectException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:25:39 26 4
gpt4 key购买 nike

我正在使用 JAX-WS 2.2.5 框架来调用 Web 服务。我想确定由于 Web 服务关闭或无法访问而导致调用失败的特殊情况。

在某些调用中,我得到一个 WebServiceException。

    catch(javax.xml.ws.WebServiceException e)
{
if(e.getCause() instanceof IOException)
if(e.getCause().getCause() instanceof ConnectException)
// Will reach here because the Web Service was down or not accessible

在其他地方,我得到 ClientTransportException(从 WebServiceException 派生的类)

    catch(com.sun.xml.ws.client.ClientTransportException ce)
{

if(ce.getCause() instanceof ConnectException)
// Will reach here because the Web Service was down or not accessible

捕获此错误的好方法是什么?

我应该使用类似的东西吗

    catch(javax.xml.ws.WebServiceException e)
{
if((e.getCause() instanceof ConnectException) || (e.getCause().getCause() instanceof ConnectException))
{
// Webservice is down or inaccessible

或者有更好的方法吗?

最佳答案

也许您还想处理 UnknownHostException!

        Throwable cause = e.getCause();

while (cause != null)
{
if (cause instanceof UnknownHostException)
{
//TODO some thing
break;
}
else if (cause instanceof ConnectException)
{
//TODO some thing
break;
}

cause = cause.getCause();
}

关于java - 在 JAX-WS web 服务调用中捕获 ConnectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com