gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:25:37 26 4
gpt4 key购买 nike

我需要访问一个安全的网络服务,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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com