我有一个 C# 代码,它基本上读取传递的 WSDL 以动态生成程序集,然后我们访问该服务的所有方法和属性。
/// <summary>
/// Builds an assembly from a web service description.
/// The assembly can be used to execute the web service methods.
/// </summary>
/// <param name="webServiceUri">Location of WSDL.</param>
/// <returns>A web service assembly.</returns>
private Assembly BuildAssemblyFromWSDL(Uri webServiceUri)
{
if (String.IsNullOrEmpty(webServiceUri.ToString()))
throw new Exception("Web Service Not Found");
XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + "?wsdl");
ServiceDescriptionImporter descriptionImporter = BuildServiceDescriptionImporter(xmlreader);
return CompileAssembly(descriptionImporter);
}
/// <summary>
/// Builds the web service description importer, which allows us to generate a proxy class based on the
/// content of the WSDL described by the XmlTextReader.
/// </summary>
/// <param name="xmlreader">The WSDL content, described by XML.</param>
/// <returns>A ServiceDescriptionImporter that can be used to create a proxy class.</returns>
private ServiceDescriptionImporter BuildServiceDescriptionImporter(XmlTextReader xmlreader)
{
// make sure xml describes a valid wsdl
if (!ServiceDescription.CanRead(xmlreader))
throw new Exception("Invalid Web Service Description");
// parse wsdl
ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader);
// build an importer, that assumes the SOAP protocol, client binding, and generates properties
ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter();
descriptionImporter.ProtocolName = "Soap";
descriptionImporter.AddServiceDescription(serviceDescription, null, null);
descriptionImporter.Style = ServiceDescriptionImportStyle.Client;
descriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
return descriptionImporter;
}
这段代码适用于所有 wsdl,除了那些受保护或安全的。代码失败, if (!ServiceDescription.CanRead(xmlreader))
因为它无法访问传递的服务 wsdl。当我尝试在浏览器中访问 url 时,我得到 500:服务器错误。当我通过正确的身份验证登录到我们的 Web 应用程序时,如果我复制 url,则使用相同的会话 - 我可以访问 wsdl。仅供参考,在另一个应用程序中,我们通过使用服务用户 ID/密码传递 SM Cookie 来动态调用此服务。
话虽如此,我该如何做同样的事情,动态访问受保护的 wsdl?我需要做哪些更改才能传递 cookie 信息以访问 wsdl?任何的想法?