ServiceDescription 未读取安全的 wsdl

发布于 2024-11-02 15:16:24 字数 2404 浏览 0 评论 0原文

我有一段 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,除了那些受保护或安全的 wsdl。该代码在 if (!ServiceDescription.CanRead(xmlreader)) 行失败,因为它无法访问传递的服务 wsdl。当我尝试在浏览器中访问 url 时,出现 500:服务器错误。当我使用正确的身份验证登录我们的 Web 应用程序,然后使用相同的会话(如果我复制 url)时,我可以访问 wsdl。仅供参考,在另一个应用程序中,我们通过传递带有服务用户 ID/密码的 SM Cookie 来动态调用此服务。

话虽如此,我怎样才能做同样的事情,动态访问受保护的 wsdl?我需要做哪些更改才能传递 cookie 信息来访问 wsdl?有什么想法吗?

I have a code in C# that basically read the passed WSDL to generate assembly dynamically and then we access all the methods and properties of that service.

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

This piece of code works for all wsdls except for those that are protected or secured. The code fails at if (!ServiceDescription.CanRead(xmlreader)) line since it is not able to access the passed service wsdl. When I try to access the url in browser I get 500: server error. And when I login to our web application with proper authentication and then with the same session if I copy the url - I can access the wsdl. Fyi, in a different application we are calling this service dynamically by passing SM Cookies with service userid/password.

With said that, how can I do the same, dynamically access the wsdl which is secured? What changes do I need to do to pass the cookie information to access the wsdl? Any idea?

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

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

发布评论

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

评论(2

残疾 2024-11-09 15:16:24
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 之前传递凭据

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

This will then allow you to pass credentials before getting the wsdl

长伴 2024-11-09 15:16:24

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

请求失败,HTTP 状态 401:未经授权。 at (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);
    }

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

真的很感谢,如果您能提供帮助...

This is really nice but after BuildAssemblyFromWSDL when i try to invoke any method it again give me an error message like..

The request failed with HTTP status 401: Unauthorized. at (T)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args);

my code to invoke a method is like...

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

Now, I don't know how to pass the credential here.. even i tried to pass that by other method but still getting un-authorized error..

Really Thanks If you can help...

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