.net core 调用 webservice

1,添加服务引用生成 客户端代码

2,开始编码

        public static String GetInfoListData(string OuCode, string ModuleId, string DateFrom, string DateTo, string CurrentPageIndex, string PageSize)
        {
            string result = "";
            // 创建 HTTP 绑定对象
            var binding = new BasicHttpBinding();
            // 根据 WebService 的 URL 构建终端点对象
            var endpoint = new EndpointAddress(webServiceUrl);
            // 创建调用接口的工厂,注意这里泛型只能传入接口 添加服务引用时生成的 webservice的接口 一般是 (XXXSoap)
            var factory = new ChannelFactory<XxgkWebService.InfoQuerySoap>(binding, endpoint);
            // 从工厂获取具体的调用实例
            var callClient = factory.CreateChannel();

            //调用的对应webservice 服务类的函数生成对应的请求类Body (一般是webservice 中对应的方法+RequestBody  如GetInfoListRequestBody)
            XxgkWebService.GetInfoListRequestBody body = new XxgkWebService.GetInfoListRequestBody();

            //以下是为该请求body添加对应的参数(就是调用webService中对应的方法的参数)
            body.OuCode = OuCode;
            body.ModuleId = ModuleId;
            body.DateFrom = DateFrom;
            body.DateTo = DateTo;
            body.CurrentPageIndex = CurrentPageIndex;
            body.PageSize = PageSize;

            //获取请求对象 (一般是webservice 中对应的方法+tRequest  如GetInfoListRequest)
            var request = new XxgkWebService.GetInfoListRequest(body);
            //发送请求
            var v = callClient.GetInfoListAsync(request);

            //异步等待
            v.Wait();

            //获取数据
            result = v.Result.Body.GetInfoListResult;


            return result;
        }

猜你喜欢

转载自blog.csdn.net/MeGoodtoo/article/details/84960020