如何在没有 WSDL 的情况下创建 SOAP 客户端

发布于 2024-08-19 09:07:13 字数 147 浏览 10 评论 0原文

我需要访问安全的网络服务, header中的每个请求都需要携带一个token。

我知道网络服务的端点, 我也知道如何创建令牌。

但我看不到 Web 服务的 WSDL。

C# 有没有办法在没有 WSDL 文件的情况下创建肥皂客户端。

i need to visit a secure web service,
every request in the header need to carry a token.

i know the endpoint to the web service,
i also know how to create the token.

but i cannot see the WSDL for the webservice.

is there a way in C#, to create a soap client, without the WSDL file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青瓷清茶倾城歌 2024-08-26 09:07:13

我已经验证此代码使用 HttpWebRequest 类,有效:

// 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 教程很有帮助。

I have verified that this code, which uses the HttpWebRequest class, works:

// 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);
        }
    }
}

You will need to create the proper SOAP envelope and pass that in as the "xml" variable. It takes some reading. I found this SOAP Tutorial to be helpful.

温柔嚣张 2024-08-26 09:07:13

SOAP 客户端只是一个包含更多内容的 HTTP 客户端。请参阅 HttpWebRequest 类。然后,您必须创建自己的 SOAP 消息,可能使用 XML 序列化。

A SOAP client is simply an HTTP client with more stuff in it. See the HttpWebRequest class. You'll then have to create your own SOAP message, perhaps using XML Serialization.

为人所爱 2024-08-26 09:07:13

您可以创建自己的服务,将其公开为具有 WSDL,然后从中生成客户端......有点漫长。

You could create your own service, expose it to have a WSDL and then generate the client from that... kind of the long way.

阪姬 2024-08-26 09:07:13

您能否要求 Web 服务的开发人员通过电子邮件向您发送 WSDL 和 XSD 文件?如果是这样,您可以将文件转储到文件夹中,然后使用硬盘上的 WSDL 添加服务引用。

Can you ask the developers of the web service to send you the WSDL and XSD file(s) by email? If so, you can dump the files in a folder and then add a Service Reference using the WSDL on your hard disk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文