.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 "成功";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值