你的位置:首页 > ASP.net教程

[ASP.net教程]csharp: get Web.Services WebMethod


using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services.Description;using System.Web.Services.Protocols;using System.Web.Services.Discovery;using System.Web.Services;using System.Net;using System.IO;using System.CodeDom;using System.CodeDom.Compiler;using Microsoft.CSharp;using System.Reflection;using System.Text;namespace WebServerProjectDemo{  /// <summary>  ///   /// </summary>  public class WebServiceHelper  {    #region InvokeWebService    /// <summary>    ///     /// </summary>    /// <param name="url"></param>    /// <param name="methodname"></param>    /// <param name="args"></param>    /// <returns></returns>    public static object InvokeWebService(string url, string methodname, object[] args)    {      return WebServiceHelper.InvokeWebService(url, null, methodname, args);    }    /// <summary>     /// 动态调用WebService     /// </summary>     /// <param name="url">WebService地址</param>     /// <param name="classname">类名</param>     /// <param name="methodname">方法名(模块名)</param>     /// <param name="args">参数列表</param>     /// <returns>object</returns>     public static object InvokeWebService(string url, string classname, string methodname, object[] args)    {      string @namespace = "ServiceBase.WebService.DynamicWebLoad";      if (classname == null || classname == "")      {        classname = WebServiceHelper.GetClassName(url);      }      //获取服务描述语言(WSDL)       WebClient wc = new WebClient();      Stream stream = wc.OpenRead(url + "?WSDL");      ServiceDescription sd = ServiceDescription.Read(stream);      ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();      sdi.AddServiceDescription(sd, "", "");      CodeNamespace cn = new CodeNamespace(@namespace);      //生成客户端代理类代码       CodeCompileUnit ccu = new CodeCompileUnit();      ccu.Namespaces.Add(cn);      sdi.Import(cn, ccu);      CSharpCodeProvider csc = new CSharpCodeProvider();      ICodeCompiler icc = csc.CreateCompiler();      //设定编译器的参数       CompilerParameters cplist = new CompilerParameters();      cplist.GenerateExecutable = false;      cplist.GenerateInMemory = true;      cplist.ReferencedAssemblies.Add("System.dll");      cplist.ReferencedAssemblies.Add("System.

  测试1

      string url = "http://www.webservicex.net/globalweather.asmx";      string[] args = new string[2];      args[0] = "shenzhen";      args[1] = "China";      object result = WebServiceHelper.InvokeWebService(url, "GetWeather", args);      this.label_Result.Text = result.ToString();

  原结果:

 

<string 

  

 

测试2

 try      {        /*         * http://webservice.web

  查询原结果:

<DataSet