以编程方式调用 ASMX 中的所有操作
Call All Operations In ASMX Programmatically
假设存在一个包含各种网络服务 URL 的通用字符串集合:
List<string> webServiceCollection = new List<string>();
webServiceCollection.Add("http://<site>/_vti_bin/Lists.asmx");
webServiceCollection.Add("http://<site>/_vti_bin/Sites.asmx");
有多种方法,每个 Web 服务中都有多种输入和 return 类型。是否可以读取所有可用操作并尝试使用默认值进行操作?为此,我正在尝试编写一个安全工具,以帮助测试 Web 服务是否允许用户在各种帐户下与他们交互,作为一项学术练习。
这将是您实现目标的最佳尝试:
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();
}
}
}
}
你可以这样做:
伪代码
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);
}
假设存在一个包含各种网络服务 URL 的通用字符串集合:
List<string> webServiceCollection = new List<string>();
webServiceCollection.Add("http://<site>/_vti_bin/Lists.asmx");
webServiceCollection.Add("http://<site>/_vti_bin/Sites.asmx");
有多种方法,每个 Web 服务中都有多种输入和 return 类型。是否可以读取所有可用操作并尝试使用默认值进行操作?为此,我正在尝试编写一个安全工具,以帮助测试 Web 服务是否允许用户在各种帐户下与他们交互,作为一项学术练习。
这将是您实现目标的最佳尝试:
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();
}
}
}
}
你可以这样做:
伪代码
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);
}