使用.net core 调用C#WebService的三种方式

WebSerrvic代码:

        [WebMethod]
        public string Test(string p1, string p2)
        {
            return p1 + "_" + p2;
        }

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Test"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Test xmlns="http://tempuri.org/">
      <p1>string</p1>
      <p2>string</p2>
    </Test>
  </soap:Body>
</soap:Envelope>

方式1:使用FormUrlEncodedContent传参数调用,内部是拼接成一个?+&的格式

   public async Task<string> Call()
   {
    Dictionary<string, string> parameters =new Dictionary<string, string>();
parameters.Add("p1","1");
parameters.Add("p2","2");
       string xmlResult = string.Empty;
       HttpContent httpContent = new FormUrlEncodedContent(parameters); 
       HttpClient httpClient = new HttpClient();
       HttpResponseMessage response = httpClient.PostAsync("http://localhost/Service1.asmx", httpContent).Result;
       var statusCode = response.StatusCode;
       if (statusCode == HttpStatusCode.OK)
       {
         xmlResult = await response.Content.ReadAsStringAsync();
       }
       return xmlResult;
   }

方式二:使用xml的格式调用

        public async Task<string> CallByXML()
        {
  Dictionary<string, string> parameters =new Dictionary<string, string>();
parameters.Add("p1","1");
parameters.Add("p2","2");
            string xmlResult = string.Empty;
            
            string xml = $@" 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Test xmlns="http://tempuri.org/">
      <p1>{parameters["p1"]}</p1>
      <p2>{parameters["p2"]}</p2>
    </Test>
  </soap:Body>
</soap:Envelope>
";

 HttpContent httpContent = new StringContent(xml, Encoding.UTF8, "text/xml");
 httpContent.Headers.Add("SOAPAction", "http://tempuri.org/Test"); 
 HttpClient httpClient = new HttpClient();
 HttpResponseMessage response = httpClient.PostAsync("http://localhost/Service1.asmx", httpContent).Result;
 var statusCode = response.StatusCode;
 if (statusCode == HttpStatusCode.OK)
 {
     xmlResult = await response.Content.ReadAsStringAsync();
 }
 return xmlResult;
  }

方式三:通过服务引用的方式调用

ServiceReference1.Service1Soap service1Soap = new ServiceReference1.Service1SoapClient
(
    ServiceReference1.Service1SoapClient.EndpointConfiguration.Service1Soap
);
var res = service1Soap.Test("1", "2").Result;

响应后的XML处理

        public string GetResultByXML(string xmlResult)
        {
            string xmlText=string.Empty;
            var xmlobj = new System.Xml.XmlDocument();
            xmlobj.LoadXml(xmlResult);
            if (!string.IsNullOrEmpty(xmlobj.InnerText))
            {
                xmlText=xmlobj.InnerText;
            }
            return xmlText;
        }

相关推荐

  1. .netcore 6 ioc注入方式

    2024-05-25 18:16:49       32 阅读
  2. 使用.net core 调用C#WebService方式

    2024-05-25 18:16:49       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-25 18:16:49       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-25 18:16:49       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-25 18:16:49       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-25 18:16:49       18 阅读

热门阅读

  1. kafka之consumer参数auto.offset.reset

    2024-05-25 18:16:49       10 阅读
  2. SpringBoot

    2024-05-25 18:16:49       12 阅读
  3. 分账系统说明

    2024-05-25 18:16:49       10 阅读
  4. 探索电子邮件的神奇世界

    2024-05-25 18:16:49       11 阅读
  5. 赶紧收藏!2024 年最常见 20道 Redis面试题(六)

    2024-05-25 18:16:49       12 阅读
  6. Spring的依赖注入

    2024-05-25 18:16:49       9 阅读
  7. JVM-调优之-高内存占用问题排查

    2024-05-25 18:16:49       10 阅读
  8. OOM不会导致JVM退出

    2024-05-25 18:16:49       8 阅读
  9. 「Electron」Electron 应用程序详解

    2024-05-25 18:16:49       11 阅读
  10. 什么是UDP服务器?

    2024-05-25 18:16:49       8 阅读