C#调用WebServices接口

编程

WebClient webClient = new WebClient();

Stream stream = webClient.OpenRead(System.Configuration.ConfigurationManager .AppSettings[ "wsServer"]);

ServiceDescription serviceDescription = ServiceDescription .Read(stream);

ServiceDescriptionImporter serviceDescriptionImporter = new ServiceDescriptionImporter ();

serviceDescriptionImporter.ProtocolName = "Soap" ;

serviceDescriptionImporter.Style = ServiceDescriptionImportStyle .Client;

serviceDescriptionImporter.CodeGenerationOptions = CodeGenerationOptions .GenerateProperties | CodeGenerationOptions .GenerateNewAsync;

serviceDescriptionImporter.AddServiceDescription(serviceDescription, null , null );

CodeNamespace codeNamespace = new CodeNamespace();

CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

codeCompileUnit.Namespaces.Add(codeNamespace);

ServiceDescriptionImportWarnings serviceDescriptionImportWarnings = serviceDescriptionImporter.Import(codeNamespace, codeCompileUnit);

CodeDomProvider codeDomProvider = CodeDomProvider .CreateProvider( "CSharp");

CompilerParameters compilerParameters = new CompilerParameters();

compilerParameters.GenerateExecutable = false ;

compilerParameters.GenerateInMemory = true ;

compilerParameters.ReferencedAssemblies.Add( "System.dll" );

compilerParameters.ReferencedAssemblies.Add( "System.XML.dll" );

compilerParameters.ReferencedAssemblies.Add( "System.Web.Services.dll" );

compilerParameters.ReferencedAssemblies.Add( "System.Data.dll" );

CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);

2.调用指定接口中的方法(传递参数等):

if(!compilerResults.Errors.HasErrors){

Assemblyassembly = compilerResults.CompiledAssembly;

Type type = assembly.GetType("IWebServicesService");

objecto =Activator.CreateInstance(type);

MethodInfo methodInfo = type.GetMethod("login");

object[] wsObject =newobject[] {this.loginName.Text,this.password.Text };

string result = methodInfo.Invoke(o, wsObject).ToString();

if(result.Equals("SUCCESS")){

return true;

}

}

 

以上是 C#调用WebServices接口 的全部内容, 来源链接: utcz.com/z/512976.html

回到顶部