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

时间:2010-01-27 22:04:13

标签: c# web-services header token ws-security

我需要访问安全的网络服务, 标题中的每个请求都需要携带一个令牌。

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

但是我看不到Web服务的WSDL。

在C#中是否有一种方法可以创建一个没有WSDL文件的soap客户端。

4 个答案:

答案 0 :(得分:5)

我已验证此代码使用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很有帮助。

答案 1 :(得分:3)

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

答案 2 :(得分:0)

您可以创建自己的服务,将其公开以获得WSDL,然后从中生成客户端...那么长的路。

答案 3 :(得分:0)

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