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

joj*_*ojo 6 c# ws-security web-services header token

我需要访问一个安全的Web服务,标头中的每个请求都需要携带一个令牌.

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

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

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

Bre*_*lle 5

我已经验证了这个使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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