1

我有一个 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?任何的想法?

4

2 回答 2

3
private Assembly BuildAssemblyFromWSDL(Uri webServiceUri)
{    
    if (String.IsNullOrEmpty(webServiceUri.ToString()))        
        throw new Exception("Web Service Not Found");    

    HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(webServiceUri.OriginalString + "?wsdl");
    wr.Credentials = new NetworkCredential("username", "password"); //replace with your credentials
    HttpWebResponse wres = (HttpWebResponse)wr.GetResponse();

    XmlTextReader xmlreader = new XmlTextReader(wres.GetResponseStream());       

    ServiceDescriptionImporter descriptionImporter = BuildServiceDescriptionImporter(xmlreader);    
    return CompileAssembly(descriptionImporter);
}

然后,这将允许您在获取 wsdl 之前传递凭据

于 2011-04-20T14:12:57.900 回答
1

这真的很好,但是在 BuildAssemblyFromWSDL 之后,当我尝试调用任何方法时,它再次给我一个错误消息,例如..

请求失败,HTTP 状态为 401:未授权。在 (T)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args);

我调用方法的代码就像......

public T InvokeMethod<T>(string serviceName, string methodName, params object[] args)
    {
        // create an instance of the specified service
        // and invoke the method
        object obj = this.webServiceAssembly.CreateInstance(serviceName);

        Type type = obj.GetType();

        return (T)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args);
    }

现在,我不知道如何在这里传递凭据..即使我尝试通过其他方法传递,但仍然出现未经授权的错误..

真的谢谢如果你能帮忙...

于 2012-07-20T12:40:40.030 回答