NetCore无需添加web/wcf引用来调用webservice

  1. 较新的WebService能支持HttpGet HttpPost SOAP1.1 SOAP1.2,这样的话和普通的http请求调用方式一样,只不过
string url = "http://localhost:5010/WebTest.asmx/PostTest"; // 参数 
Dictionary<string, string> dicParam = new Dictionary<string, string>();
 dicParam.Add("para", "1"); // 将参数转化为HttpContent 
HttpContent content = new FormUrlEncodedContent(dicParam);
来源: https://my.oschina.net/u/4373790/blog/3220683

2.比较老的WebService 只支持SOAP,需做如下操作,以天气预报为例

  • 打开asmx的页面查看Envelope结构
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getSupportCity"

<?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>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>string</byProvinceName>
    </getSupportCity>
  </soap:Body>
</soap:Envelope>

按此建立XML

 XNamespace ns = "http://www.w3.org/2003/05/soap-envelope";
                XNamespace myns = "http://WebXml.com.cn/";
                XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
                XDocument soapRequest = new XDocument(
                    new XDeclaration("1.0", "UTF-8", "no"),
                    new XElement(ns + "Envelope",
                        new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                        new XAttribute(XNamespace.Xmlns + "xsd", xsd),
                        new XAttribute(XNamespace.Xmlns + "soap", ns),
                        new XElement(ns + "Body",
                            new XElement(myns + "getSupportCityString",
                                               new XElement(myns + "theRegionCode", $"{code}")
                            )
                        )
                    ));

注意 坑来了,asmx这个页面只能看结构,具体提供的方法和参数要看?wsdl的页面

  private async Task<string> PostHelper(string url,XDocument xml)
        {
            
            var result = string.Empty;
            try
            {
                using (var client = _httpClientFactory.CreateClient())
                {
                    var request = new HttpRequestMessage()
                    {
                        RequestUri = new Uri(url),
                        Method = HttpMethod.Post
                    };
                    request.Content = new StringContent(xml.ToString(), Encoding.UTF8, "text/xml");
                    request.Headers.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
                    request.Headers.Add("SOAPAction", "http://WebXml.com.cn/getSupportCityString");
                    HttpResponseMessage response = client.SendAsync(request).Result;
                    result = await response.Content.ReadAsStringAsync();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(result);
                    }
                    else
                    {
                        return result;
                    }
                }
              
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
        }

好了 结束

posted @ 2020-12-24 15:12  那年的夏天  阅读(500)  评论(1编辑  收藏  举报