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

摘要:
xmlversion=“1.0”encoding=“utf-8”?
  1. 较新的WebService能支持HttpGetHttpPostSOAP1.1SOAP1.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;
        }

好了 结束

免责声明:文章转载自《NetCore无需添加web/wcf引用来调用webservice》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SendMessage模拟按键所需的虚拟键码flex布局学习笔记下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Npoi XWPF Word 导出时插入图片无法显示

npoi中XWPFRun.AddPicture,各种尝试,各种看源代码,也无法将插入的图片显示出来,用RAR程序打开word查看Document.xml文件,提示xml文件错误.在网上找到java的poi的解决办法,自定义Pic元素. int EMU = 9525; width *= EMU; he...

WPF学习笔记:ComboBox的数据绑定

UI <UserControl x: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"...

XML、Linq、序列化、反射、XSLT在测试报告统计工具中的应用 Anny

客户要求对XML格式的测试结果进行统计,并发送出特定格式的邮件。 1. 测试结果有两种文件,一种是以xml为后缀的xml格式文件,一种是VS里跑测试用例产生的以trx为后缀的xml格式文件。 2. 每个文件里包含多个测试用例,每个测试用例有对应的Owner和归属类别Area。 3. 最后输出的报告格式如下: Area File Owner Pass F...

我的WCF之旅 (11): 再谈WCF的双向通讯基于Http的双向通讯 V.S. 基于TCP的双向通讯

在一个基于面向服务的分布式环境中,借助一个标准的、平台无关的Communication Infrastructure,各个Service通过SOAP Message实现相互之间的交互。这个交互的过程实际上就是Message Exchange的过程。WCF支持不同形式的Message Exchange,我们把这称之为Message Exchange Patt...

WCF中的数据契约(Continued)

版本化数据契约 变化是不可避免的。业务的改变,技术的改变,规则的改变,这些都会造成软件契约的变化。面对软件的变化,一个稳定的版本策略是必不可少的。必须注意对不可避免的变化进行预先的筹划,并对当前的客户端保证向后的兼容性。 最常见的对版本化契约的需求是,当现有数据契约中添加新的成员时。按照不间断的版本更改的描述,你可以自由更改,而不需要终止现有的客户端。但...

Dataset利用xsd读取xml,数值类型处理及验证

应该会有很多场景需要从xml文件读取数据,填充一个dataset。机器上没装数据库,个人觉得最好简便方法就是定义一个xml文件,模拟数据。默认,xml在datatable中的值都是字符串类型(Excel中输入数字,就知道是数值型)。如果需要dataset在调用readxml方法的时候,把是什么类型(比如xml中本意是整型,时间类型)自动转换成什么类型方便,...