.net core使用post请求动态访问WebService接口

.net core使用post请求动态访问WebService接口

        public string CallWebService(string soapText, string webWebServiceUrl)
        {
            using (WebClient webClient = new WebClient())
            {
                try
                {
                    webClient.Proxy = null;
                    byte[] postDatabyte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(soapText);

                    webClient.Headers.Add("Content-Type", "text/xml");
                    byte[] responseData = webClient.UploadData(webWebServiceUrl, "POST", postDatabyte);
                    //解码 
                    string responsestring = System.Text.Encoding.GetEncoding("UTF-8").GetString(responseData);
                    LogHelper.Warn("webservice请求成功!--" + soapText);
                    return responsestring;
                }
                catch (Exception e)
                {
                    LogHelper.Error("webservice请求错误!", e);
                    return null;
                }
            }

        }

使用方法

webservice地址是不带wsdl的,比如http://localhost:5005/RemoteMonitorService.asmx
请求参数soapText必须是符合Soap请求标准格式比如

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:RmServiceReceiveHIS>
         <!--参数1-->
         <tem:XmlTypeCode>?</tem:XmlTypeCode>
         <!--参数2-->
         <tem:xmlParams>?</tem:xmlParams>
      </tem:RmServiceReceiveHIS>
   </soapenv:Body>
</soapenv:Envelope>

调用示例

            WebServiceHelper webServiceHelper = new WebServiceHelper();
            var soapText = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:RmServiceReceiveHIS><!--Optional:--><tem:XmlTypeCode>?</tem:XmlTypeCode><!--Optional:--><tem:xmlParams>?</tem:xmlParams></tem:RmServiceReceiveHIS></soapenv:Body></soapenv:Envelope>";
            var result= webServiceHelper.CallWebService(soapText, "http://localhost:5005/RemoteMonitorService.asmx");
            return "成功";
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Web Service接口是一种在.NET开发环境中用于实现分布式计算的技术。它基于SOAP(Simple Object Access Protocol)协议,使用HTTP协议进行通信,并通过XML(eXtensible Markup Language)进行数据交换。 .NET Web Service接口使得不同平台和技术之间的系统可以进行互操作。通过使用WebService接口,我们可以实现跨平台的应用程序集成,使得不同系统之间能够方便地进行数据交互和功能调用。 .NET Web Service接口的开发需要使用.NET框架提供的工具和类库。开发人员可以使用.NET编程语言(如C#或VB.NET)编写WebService代码,并通过.NET开发工具(如Visual Studio)进行开发、调试和部署。 WebService接口的开发过程一般包括以下步骤: 1. 定义接口:明确定义WebService接口的方法和参数,以及返回值的类型。 2. 实现接口:根据接口定义,实现具体的功能代码,处理业务逻辑。 3. 配置服务:通过配置文件,指定WebService接口访问地址、安全性设置等相关信息。 4. 发布服务:将WebService部署到服务器上,使其可以通过网络访问。 5. 编写客户端:根据WebService接口的定义,编写客户端程序代码,通过调用WebService的方法来实现功能。 .NET Web Service接口具有以下的优点: 1. 跨平台:WebService接口可以在不同操作系统和编程语言的环境中进行调用,方便实现跨平台的集成和系统间的数据交换。 2. 松耦合:WebService接口的调用是通过网络进行的,系统之间没有强耦合的依赖关系,可以独立部署和维护。 3. 可扩展性:由于使用了标准的Web协议和格式,WebService接口可以很容易地扩展和添加新功能。 4. 安全性:可以通过配置安全认证和访问控制来保护WebService接口的安全性。 总之,.NET Web Service接口是一种方便实现分布式系统集成和跨平台数据交换的技术,可以广泛应用于各种软件开发项目中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值