以编程方式调用ASMX中的所有操作

时间:2018-07-18 14:38:41

标签: c# asmx

假设存在一个通用字符串集合,其中包含各种Web服务URL:

List<string> webServiceCollection = new List<string>();
webServiceCollection.Add("http://<site>/_vti_bin/Lists.asmx");
webServiceCollection.Add("http://<site>/_vti_bin/Sites.asmx");

有各种方法,每个Web服务中都有各种输入和返回类型。是否可以读取所有可用的操作并尝试使用默认值进行操作?为此,我正在尝试编写一个安全工具,以帮助测试网络服务是否允许用户在各种帐户下与他们进行交互,这是一项学术练习。

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

伪代码

    System.Net.WebClient client = new System.Net.WebClient(); 
    string strUrl = @"http://localhost:xxxxx/Service.asmx?wsdl"; 
    System.IO.Stream stream = client.OpenRead(strUrl); 

    string serviceName = "Service"; 

    // Get a WSDL file describing a service. 
    ServiceDescription description = ServiceDescription.Read(stream); 

    ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); 
    importer.ProtocolName = "Soap12";  // Use SOAP 1.2. 
    importer.AddServiceDescription(description, null, null); 

    // Generate a proxy client. 
    importer.Style = ServiceDescriptionImportStyle.Client; 

    // Generate properties to represent primitive values. 
    importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; 

    // Initialize a Code-DOM tree into which we will import the service. 
    CodeNamespace nmspace = new CodeNamespace(); 
    CodeCompileUnit unit1 = new CodeCompileUnit(); 
    unit1.Namespaces.Add(nmspace); 

    // Import the service into the Code-DOM tree. This creates proxy code 
    // that uses the service. 
    ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1); 

     if (warning == 0) 
     { 
        // Generate and print the proxy code in C#. 
        CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp"); 

        // Compile the assembly with the appropriate references 
        string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" }; 
        CompilerParameters parms = new CompilerParameters(assemblyReferences); 
        CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1); 

        foreach (CompilerError  oops in results.Errors) 
        { 
            Console.WriteLine("========Compiler error============"); 
            Console.WriteLine(oops.ErrorText); 
        } 

        //Invoke the web service method 
        foreach (PortType portType in description.PortTypes)
        {
            foreach (Operation operation in portType.Operations)
            {
                try
                {
                    object o = results.CompiledAssembly.CreateInstance(serviceName); 
                    Type t = o.GetType(); 
                    Console.WriteLine(t.InvokeMember(operation.Name, System.Reflection.BindingFlags.InvokeMethod, null, o, null)); 
                }catch(Exception ex){
                    Console.WriteLine(ex.Message);
                }
            }
        }
     } 

    else 
    { 
        // Print an error message. 
        Console.WriteLine("Warning: " + warning); 
    } 

答案 1 :(得分:0)

这是您尝试实现目标的最佳尝试:

string completeUrl ="<a href="http://localhost/testwebservice">http://localhost/testwebservice</a>";

// Create a request for the URL.         
WebRequest request = WebRequest.Create(completeUrl);

// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;


    // If you have a proxy configured.
    WebProxy proxyObject = new WebProxy("<a href="http://proxy.com/">http://proxy.com/</a>", true);
    request.Proxy = proxyObject;


    //Get the response.
    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
      // Get the stream containing content returned by the server.
      using(Stream dataStream = response.GetResponseStream())
      {
        // Open the stream using a StreamReader for easy access.
        using(StreamReader reader = new StreamReader(dataStream))
        {
           // Read the content.
           string responseFromServer = reader.ReadToEnd();   
        }
      }
    }

并且为了添加您的尝试,您应该这样做:

List<string> webServiceCollection = new List<string>();
webServiceCollection.Add("http://<site>/_vti_bin/Lists.asmx");
webServiceCollection.Add("http://<site>/_vti_bin/Sites.asmx");

  foreach(var reqeust in webServiceCollection) 
  {
    string completeUrl = $"<a href="request">{request}</a>";;

    // Create a request for the URL.         
    WebRequest request = WebRequest.Create(completeUrl);

    // If required by the server, set the credentials.
    request.Credentials = CredentialCache.DefaultCredentials;


    // If you have a proxy configured.
    WebProxy proxyObject = new WebProxy("<a href="http://proxy.com/">http://proxy.com/</a>", true);
    request.Proxy = proxyObject;


    //Get the response.
    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
      // Get the stream containing content returned by the server.
      using(Stream dataStream = response.GetResponseStream())
      {
        // Open the stream using a StreamReader for easy access.
        using(StreamReader reader = new StreamReader(dataStream))
        {
           // Read the content.
           string responseFromServer = reader.ReadToEnd();   
        }
      }
    }
  }