加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在没有WSDL的情况下创建soap客户端

发布时间:2020-12-15 08:36:12 所属栏目:百科 来源:网络整理
导读:我需要访问一个安全的网络服务, 标头中的每个请求都需要携带令牌. 我知道Web服务的端点, 我也知道如何创建令牌. 但我无法看到Web服务的WSDL. 在C#中有没有办法创建一个没有WSDL文件的soap客户端. 解决方法 我已经验证这个使用 HttpWebRequest class的代码有
我需要访问一个安全的网络服务,
标头中的每个请求都需要携带令牌.

我知道Web服务的端点,
我也知道如何创建令牌.

但我无法看到Web服务的WSDL.

在C#中有没有办法创建一个没有WSDL文件的soap客户端.

解决方法

我已经验证这个使用 HttpWebRequest class的代码有效:
// Takes an input of the SOAP service URL (url) and the XML to be sent to the
// service (xml).  
public void PostXml(string url,string xml) 
{
    byte[] bytes = Encoding.UTF8.GetBytes(xml);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentLength = bytes.Length;
    request.ContentType = "text/xml";

    using (Stream requestStream = request.GetRequestStream())
    {
       requestStream.Write(bytes,bytes.Length);
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("POST failed with HTTP {0}",response.StatusCode);
            throw new ApplicationException(message);
        }
    }
}

您需要创建正确的SOAP信封并将其作为“xml”变量传递.这需要一些阅读.我发现这个SOAP Tutorial很有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读