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

前端之家收集整理的这篇文章主要介绍了c# – 如何在没有WSDL的情况下创建soap客户端前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要访问一个安全的网络服务,
标头中的每个请求都需要携带令牌.

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

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

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

解决方法

我已经验证这个使用 HttpWebRequest class代码有效:
  1. // Takes an input of the SOAP service URL (url) and the XML to be sent to the
  2. // service (xml).
  3. public void PostXml(string url,string xml)
  4. {
  5. byte[] bytes = Encoding.UTF8.GetBytes(xml);
  6. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  7. request.Method = "POST";
  8. request.ContentLength = bytes.Length;
  9. request.ContentType = "text/xml";
  10.  
  11. using (Stream requestStream = request.GetRequestStream())
  12. {
  13. requestStream.Write(bytes,bytes.Length);
  14. }
  15.  
  16. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  17. {
  18. if (response.StatusCode != HttpStatusCode.OK)
  19. {
  20. string message = String.Format("POST Failed with HTTP {0}",response.StatusCode);
  21. throw new ApplicationException(message);
  22. }
  23. }
  24. }

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

猜你在找的C#相关文章