终于会使用异步调用ws了,高兴啊

ws端
<WebMethod(Description:="异步调用演示")> _
    Public Function HelloWorld(ByVal temp As String) As String
        Return temp
    End Function

客户端定义的新类

Public Class Class1
    Private m_WsData As New localhost.Service1


    Public Function beginhellow() As IAsyncResult
        Try
            Return m_WsData.BeginHelloWorld("下午好", Nothing, New Object)
        Catch ex As Exception
            MessageBox.Show("beginhellow发生错误!" & ex.Message & vbNewLine & ex.StackTrace)
            Return Nothing
        End Try
    End Function

    Public Function Endhellow(ByVal ar As IAsyncResult) As String
        Dim temp As String
        Try
            Return m_WsData.EndHelloWorld(ar)
        Catch ex As Exception
            Return "Endhellow发生错误!" & ex.Message & ex.StackTrace
        End Try
    End Function
End Class

客户端使用

Dim temp As New Class1
        Dim ar As IAsyncResult
        ar = temp.beginhellow()
        'ar.AsyncWaitHandle.WaitOne()

        
        If (ar.IsCompleted) Then
            Dim result As String
            result = temp.Endhellow(ar)
            TextBox1.Text = result
        End If

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java异步调用Web Service的方法之一是使用JAX-WS异步调用机制。以下是一个示例代码: ```java import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; import javax.xml.ws.WebServiceException; import javax.xml.ws.soap.SOAPFaultException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; public class AsyncWebServiceClient { public static void main(String[] args) { // 创建服务对象 MyWebService service = new MyWebService(); // 获取服务实现类的代理对象 MyWebServicePortType port = service.getMyWebServicePort(); // 异步调用服务接口 Future<?> future = port.myWebServiceAsync(param1, param2, new AsyncHandler<MyWebServiceResponse>() { @Override public void handleResponse(Response<MyWebServiceResponse> response) { try { // 处理异步响应结果 MyWebServiceResponse result = response.get(); // TODO: 处理异步返回结果 } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { if (e.getCause() instanceof SOAPFaultException) { SOAPFaultException sfe = (SOAPFaultException) e.getCause(); // TODO: 处理SOAPFaultException异常 } else { e.printStackTrace(); } } catch (WebServiceException e) { e.printStackTrace(); } } }); // 异步调用后,可以在此处执行其他业务逻辑,或等待异步调用结果 // ... } } ``` 其中,`MyWebService` 是服务端提供的 Web Service 接口,`MyWebServicePortType` 是接口的代理对象,`myWebServiceAsync` 是异步调用的方法。在异步调用方法中,可以传入回调函数 `AsyncHandler` 处理响应结果。在回调函数中,可以通过 `response.get()` 获取异步调用的返回结果。 需要注意的是,在回调函数中处理异常时,可能抛出 `SOAPFaultException` 异常,需要特别处理。此外,还需要处理其他可能的异常,如 `InterruptedException` 和 `WebServiceException`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值