In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Steps when winform calls webservice in general
1 add service reference-Advanced-add web reference-fill in url-- and add web reference to complete the reference to webservice
Let the VS.NET environment generate the service proxy for us, and then invoke the corresponding Web service.
If you need to call WebService dynamically, you can do this:
Public static object InvokeWebService (string url, string methodname,object [] args)
Where url is the address of the Web service, methodname is the name of the method to invoke the service, args is the parameter required to invoke the Web service, and the return value is the result returned by the web service.
To achieve this, you need skills in these areas: reflection, CodeDom, programming using the C # compiler, and WebService. With this knowledge, you can easily implement dynamic invocation of web services:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.IO
Using System.Net
Using System.CodeDom
Using System.CodeDom.Compiler
Using System.Web.Services.Description
Using Microsoft.CSharp
Namespace NetbankTMP
{
Public static class WebServiceHelper
{
/ / /
/ / dynamically call WebService
/ / /
/ WebService address
/ / method name (module name)
/ / Parameter list. No parameter is null.
/ object
Public static object InvokeWebService (string url, string methodname, object [] args)
{
Return InvokeWebService (url, null, methodname, args)
}
/ / /
/ / dynamically call WebService
/ / /
/ WebService address
/ / Class name
/ / method name (module name)
/ / Parameter list
/ object
Public static object InvokeWebService (string url, string classname, string methodname, object [] args)
{
String @ namespace = "fangqm.Netbank.WebService.webservice"
If (classname = = null | | classname = = "")
{
Classname = WebServiceHelper.GetClassName (url)
}
/ / get Service description language (WSDL)
WebClient wc = new WebClient ()
Stream stream = wc.OpenRead (url+ "? WSDL"); / / [1]
ServiceDescription sd = ServiceDescription.Read (stream); / / [2]
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter (); / / [3]
Sdi.AddServiceDescription (sd, ",")
CodeNamespace cn = new CodeNamespace (@ namespace); / / [4]
/ / generate client agent class code
CodeCompileUnit ccu = new CodeCompileUnit (); / / [5]
Ccu.Namespaces.Add (cn)
Sdi.Import (cn, ccu)
CSharpCodeProvider csc = new CSharpCodeProvider (); / / [6]
ICodeCompiler icc = csc.CreateCompiler (); / / [7]
/ / set compiler parameters
CompilerParameters cplist = new CompilerParameters (); / / [8]
Cplist.GenerateExecutable = false
Cplist.GenerateInMemory = true
Cplist.ReferencedAssemblies.Add ("System.dll")
Cplist.ReferencedAssemblies.Add ("System.XML.dll")
Cplist.ReferencedAssemblies.Add ("System.Web.Services.dll")
Cplist.ReferencedAssemblies.Add ("System.Data.dll")
/ / compile the proxy class
CompilerResults cr = icc.CompileAssemblyFromDom (cplist, ccu); / / [9]
If (true = = cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new StringBuilder ()
Foreach (CompilerError ce in cr.Errors)
{
Sb.Append (ce.ToString ())
Sb.Append (System.Environment.NewLine)
}
Throw new Exception (sb.ToString ())
}
/ / generate a proxy instance and call the method
System.Reflection.Assembly assembly = cr.CompiledAssembly
Type t = assembly.GetType (@ namespace+ "." + classname, true, true)
Object bj = Activator.CreateInstance (t); / / [10]
System.Reflection.MethodInfo mi = t.GetMethod (methodname); / / [11]
Return mi.Invoke (obj, args)
}
Private static string GetClassName (string url)
{
/ / if URL is "http://localhost/InvokeService/Service1.asmx""
/ / the final return value is Service1
String [] parts = url.Split ('/')
String [] pps = parts [parts.Length-1] .split ('.')
Return pps [0]
}
}
}
The above comments have well illustrated the functions of each code snippet. Let's take a look at an example, which is to access the http://www.webservicex.net/globalweather.asmx service to get the weather conditions of each major city.
String url= "http://www.webservicex.net/globalweather.asmx";
String [] args=newstring [2]
Args [0] = this.textBox_CityName.Text
Args [1] = "China"
Object result=WebServiceHelper.InvokeWebService (url, "GetWeather", args)
This.label_Result.Text=result.ToString ()
If there is no parameter, the parameter is null
In the above example, the invocation of the web service takes two parameters, the first is the name of the city and the second is the name of the country, and the Web service returns an XML document from which the temperature, wind and other weather conditions can be parsed.
Comments about this code
[2] the ServiceDescription class provides a way to create and format a valid Web Services description language (WSDL) document file for describing XML Web services, which is complete with appropriate namespaces, elements, and features. Cannot inherit this class.
The ServiceDescription.Read method (Stream) initializes an instance of the ServiceDescription class by loading XML directly from the Stream instance.
[3] the ServiceDescriptionImporter class exposes a way to generate client-side proxy classes for XML Web services.
The ServiceDescriptionImporter.AddServiceDescription method adds the specified ServiceDescription to the collection of ServiceDescripts values to import.
[4] CodeNamespace represents a namespace declaration.
[5] CodeCompileUnit provides a circular container for CodeDOM programs, and CodeCompileUnit contains a collection that stores CodeNamespace objects that contain CodeDOM source code prototypes, assembly collections referenced by the project, and collection of project assembly properties.
[6] the CSharpCodeProvider class provides access to instances of the C # code generator and the code compiler.
[7] get the instance of the C # code compiler
[8] create a parameter instance of the compiler
[9] CompilerResults represents the compilation result returned from the compiler. Compiles a component based on the System.CodeDom tree structure contained in the specified array of CodeCompileUnit objects, using the specified compiler settings.
[10] the Activator class contains specific methods to create object types locally or remotely, or to obtain references to existing remote objects. Cannot inherit this type of Activator.CreateInstance method to create an instance of the specified type using the constructor that best matches the specified parameter.
[11] an instance of MethodInfo can be obtained by calling the GetMethod method of a GetMethods or Type object or an object derived from Type, or by calling a MakeGenericMethod method that represents the MethodInfo defined by a generic method.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Why do I need to establish a DevSecOps?
© 2024 shulou.com SLNews company. All rights reserved.