http://www./WebServices/WeatherWebService.asmx
里面有天氣的圖標。
添加WebServiceHelper.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Configuration;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Net;
- using System.Web.Services;
- using System.Web.Services.Description;
- using Microsoft.CSharp;
-
-
- namespace ZJPortGisLib
- {
- public static class WebServiceHelper
- {
-
- /// <summary>
-
- /// 動態(tài)調(diào)用WebService
-
- /// </summary>
-
- /// <param name="url">WebService地址</param>
-
- /// <param name="methodname">方法名(模塊名)</param>
-
- /// <param name="args">參數(shù)列表</param>
-
- /// <returns>object</returns>
-
- public static object InvokeWebService(string url, string methodname, object[] args)
- {
-
- return InvokeWebService(url, null, methodname, args);
-
- }
-
- /// <summary>
-
- /// 動態(tài)調(diào)用WebService
-
- /// </summary>
-
- /// <param name="url">WebService地址</param>
-
- /// <param name="classname">類名</param>
-
- /// <param name="methodname">方法名(模塊名)</param>
-
- /// <param name="args">參數(shù)列表</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();
-
- //設定編譯器的參數(shù)
-
- CompilerParameters cplist = new CompilerParameters();
-
- 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");
-
- //編譯代理類
-
- CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
-
- 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());
- }
- //生成代理實例,并調(diào)用方法
-
- System.Reflection.Assembly assembly = cr.CompiledAssembly;
-
- Type t = assembly.GetType(@namespace + "." + classname, true, true);
-
- object obj = Activator.CreateInstance(t);
- System.Reflection.MethodInfo mi = t.GetMethod(methodname);
-
- return mi.Invoke(obj, args);
- }
-
- private static string GetClassName(string url)
- {
- string[] parts = url.Split('/');
- string[] pps = parts[parts.Length - 1].Split('.');
- return pps[0];
- }
- }
-
- }
調(diào)用
- //天氣初始化
- private void WeatherLoad()
- {
- string url = "http://www./WebServices/WeatherWebService.asmx";
- string[] arg = new string [1];
- arg[0] = "杭州";
- try
- {
- object weather = ZJPortGisLib.WebServiceHelper.InvokeWebService(url, "getWeatherbyCityName", arg);
- string[] h = (string[])weather;
- this.Literal1.Text = "<li><img src="Images/weather/a_" + h[9] + "" mce_src="Images/weather/a_" + h[9] + "" style='width:20px;height:20px;' /></li><li>" + Weather(h[6]) + "</li><li>" + WeatherTemp(h[5]) + "</li>";
- }
- catch (Exception ex)
- {
- }
- }
-
- //切分天氣文字
- private string Weather(string wea)
- {
- string[] arr = wea.Split(' ');
- if (arr.Length == 2)
- {
- return arr[1].ToString().Trim();
- }
- return "";
- }
-
- //切分溫度
- private string WeatherTemp(string tem)
- {
- string[] arr = tem.Split('/');
- if (arr.Length == 2)
- {
- return arr[0].ToString().Trim() + "-" + arr[1].ToString().Trim();
- }
- return "";
- }
|