作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要访问一个安全的网络服务,header中的每个请求都需要携带一个token。
我知道网络服务的端点,我也知道如何创建 token 。
但我看不到网络服务的 WSDL。
有没有办法在 C# 中创建一个 soap 客户端,而无需 WSDL 文件。
最佳答案
我已经验证了这段代码,它使用了 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, 0, 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有帮助。
关于c# - 如何在没有 WSDL 的情况下创建 soap 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2150621/
我是一名优秀的程序员,十分优秀!