如何在VS2015中使用WSE WebService?

时间:2018-04-04 14:40:39

标签: c# web-services visual-studio-2015 wse

我正在尝试连接到Ohio Finder服务以获取本地销售税信息。该服务的文档和端点地址可通过以下方式获得:

https://thefinder.tax.ohio.gov/streamlinesalestaxweb/webservice/about.aspx

该服务可免费使用,但需要用户注册。

为了添加服务引用,我按照以下步骤操作:

  1. 右键单击引用,然后选择“添加服务引用”
  2. 点击“高级”以打开“服务参考设置”
  3. 单击“兼容性”部分下的“添加Web引用”。
  4. 提供服务端点“https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx”并单击以发现服务。在URL上找到了OHFinderService,因此我添加了引用。
  5. 引用已成功添加但我缺少用于对服务进行身份验证的WSE对象。

    我尝试在配置部分添加以下配置:

    <webServices>
          <soapExtensionImporterTypes>
            <add type="Microsoft.Web.Services3.Description.WseExtensionImporter, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          </soapExtensionImporterTypes>
          <soapServerProtocolFactory type="Microsoft.Web.Services3.WseProtocolFactory, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
     </webServices>
    

    添加配置部分后,我点击了“更新Web参考”,但我没有获得WSE对象。

    关于如何使用VS2015连接到传统WSE服务的任何想法?

    更新 我没有运气让WSE对象出现在visual studio中。这里的最终目标是连接到服务并获取费率信息。考虑到这一点,我正在尝试自己构建SOAP请求的并行路径。到目前为止,我在发出请求时收到内部服务器错误。服务的规范可以在上面链接的站点中找到。

    以下是我根据请求发送的XML。

    <?xml version="1.0" encoding="utf-8"?>
    <SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:si="http://soapinterop.org/xsd"
                       xmlns:ns5925="https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx"
                       xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
        <SOAP-ENV:Header>
            <wsse:Security>
                <wsse:UsernameToken>
                    <wsse:Username>MYUSERNAME</wsse:Username>
                    <wsse:Password Type="wsse: PasswordText">MYPASSWORD</wsse:Password>
                </wsse:UsernameToken>
            </wsse:Security>
        </SOAP-ENV:Header> 
        <SOAP-ENV:Body>
            <GetOHSalesTaxByZipCode xmlns="https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx">
                <PostalCode>43230</PostalCode><SalesAmount>10.00</SalesAmount><SalesDate>4%2f5%2f2018</SalesDate><ReturnMultiple>false</ReturnMultiple>
            </GetOHSalesTaxByZipCode>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    以下是我用来构建XML的代码

    public class WebServiceHelper
    {
        public string Url { get; set; }
        public string MethodName { get; set; }
        public Dictionary<string, string> Params = new Dictionary<string, string>();
        public XDocument ResultXML;
        public string ResultString;
    
        public WebServiceHelper()
        {
    
        }
    
        public WebServiceHelper(string url, string methodName)
        {
            Url = url;
            MethodName = methodName;
        }
    
        /// <summary>
        /// Invokes service
        /// </summary>
        public void Invoke()
        {
            Invoke(true);
        }
    
        /// <summary>
        /// Invokes service
        /// </summary>
        /// <param name="encode">Added parameters will encode? (default: true)</param>
        public void Invoke(bool encode)
        {
            string soapStr =
                @"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
                    <SOAP-ENV:Envelope xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                                       xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""  
                                       xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
                                       xmlns:si=""http://soapinterop.org/xsd""
                                       xmlns:ns5925=""https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx""
                                       xmlns:wsse=""http://schemas.xmlsoap.org/ws/2002/07/secext"">
                <SOAP-ENV:Header>
                    <wsse:Security>
                        <wsse:UsernameToken>
                            <wsse:Username>USERNAMEHERE</wsse:Username>
                            <wsse:Password Type=""wsse: PasswordText"">PASSWORDHERE</wsse:Password>
                        </wsse:UsernameToken>
                    </wsse:Security>
               </SOAP-ENV:Header> 
               <SOAP-ENV:Body>
                <{0} xmlns=""https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx"">
                  {1}
                </{0}>
              </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>";
    
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
            req.Headers.Add("SOAPAction", "\"https://thefinder.tax.ohio.gov/OHFinderService/" + MethodName + "\"");
            req.ContentType = "text/xml;charset=\"utf-8\"";
            req.Accept = "text/xml";
            req.Method = "POST";
    
            using (Stream stm = req.GetRequestStream())
            {
                string postValues = "";
                foreach (var param in Params)
                {
                    if (encode)
                        postValues += string.Format("<{0}>{1}</{0}>", HttpUtility.UrlEncode(param.Key), HttpUtility.UrlEncode(param.Value));
                    else
                        postValues += string.Format("<{0}>{1}</{0}>", param.Key, param.Value);
                }
    
                soapStr = string.Format(soapStr, MethodName, postValues);
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soapStr);
                }
            }
    
            using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                string result = responseReader.ReadToEnd();
                ResultXML = XDocument.Parse(result);
                ResultString = result;
            }
        }
    }
    

    以下是我调用此代码的方式:

                WebServiceHelper ws = new WebServiceHelper("https://thefinder.tax.ohio.gov/OHFinderService/OHFinderService.asmx", "GetOHSalesTaxByZipCode");
            ws.Params.Add("PostalCode", "43230");
            ws.Params.Add("SalesAmount", "10.00");
            ws.Params.Add("SalesDate", DateTime.Today.ToShortDateString());
            ws.Params.Add("ReturnMultiple", "false");
            ws.Invoke();
    

    有人能指出我在正确的方向吗?我的猜测是我正在构建错误的请求......

1 个答案:

答案 0 :(得分:1)

自Visual Studio 2008以来,Web服务增强功能3(WSE 3)未得到官方支持。

我们必须安装WSE 3.0,然后手动集成到Visual Studio中。

请使用此link访问VS 2015中的wse服务。