一般情況下winform調(diào)用webservice時(shí)步驟
1添加服務(wù)引用---高級(jí)----添加web引用------填寫(xiě)url--添加web引用即可完成對(duì)webservice的引用
讓VS.NET環(huán)境來(lái)為我們生成服務(wù)代理,然后調(diào)用對(duì)應(yīng)的Web服務(wù)。
如果需要?jiǎng)討B(tài)調(diào)用WebService,要實(shí)現(xiàn)這樣的功能:
public static object InvokeWebService(string url, string methodname,object[] args)
其中,url是Web服務(wù)的地址,methodname是要調(diào)用服務(wù)方法名,args是要調(diào)用Web服務(wù)所需的參數(shù),返回值就是web服務(wù)返回的結(jié)果了。
要實(shí)現(xiàn)這樣的功能,你需要這幾個(gè)方面的技能:反射、CodeDom、編程使用C#編譯器、WebService。在了解這些知識(shí)后,就可以容易的實(shí)現(xiàn)web服務(wù)的動(dòng)態(tài)調(diào)用了:
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
{
/// <summary>
/// 動(dòng)態(tài)調(diào)用WebService
/// </summary>
/// <param name="url">WebService地址</param>
/// <param name="methodname">方法名(模塊名)</param>
/// <param name="args">參數(shù)列表,無(wú)參數(shù)為null</param>
/// <returns>object</returns>
public static object InvokeWebService(string url, string methodname, object[] args)
{
return InvokeWebService(url, null, methodname, args);
}
/// <summary>
/// 動(dòng)態(tài)調(diào)用WebService
/// </summary>
/// <param name="url">WebService地址</param>
/// <param name="classname">類(lèi)名</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 = "fangqm.Netbank.WebService.webservice";
if (classname == null || classname == "")
{
classname = WebServiceHelper.GetClassName(url);
}
//獲取服務(wù)描述語(yǔ)言(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】
//生成客戶端代理類(lèi)代碼
CodeCompileUnit ccu = new CodeCompileUnit();//【5】
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();//【6】
ICodeCompiler icc = csc.CreateCompiler();//【7】
//設(shè)定編譯器的參數(shù)
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");
//編譯代理類(lèi)
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());
}
//生成代理實(shí)例,并調(diào)用方法
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)
{
//假如URL為"http://localhost/InvokeService/Service1.asmx"
//最終的返回值為 Service1
string[] parts = url.Split('/');
string[] pps = parts[parts.Length - 1].Split('.');
return pps[0];
}
}
}
上面的注釋已經(jīng)很好的說(shuō)明了各代碼段的功能,下面給個(gè)例子看看,這個(gè)例子是通過(guò)訪問(wèn)http://www.webservicex.net/globalweather.asmx服務(wù)來(lái)獲取各大城市的天氣狀況。
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() ;
如果沒(méi)有參數(shù),則參數(shù)為null
上述的例子中,調(diào)用web服務(wù)使用了兩個(gè)參數(shù),第一個(gè)是城市的名字,第二個(gè)是國(guó)家的名字,Web服務(wù)返回的是XML文檔,可以從其中解析出溫度、風(fēng)力等天氣情況。
關(guān)于這段代碼的注釋
【2】 ServiceDescription類(lèi)提供一種方法,以創(chuàng)建和格式化用于描述 XML Web services 的有效的 Web 服務(wù)描述語(yǔ)言 (WSDL) 文檔文件,該文件是完整的,具有適當(dāng)?shù)拿臻g、元素和特性。無(wú)法繼承此類(lèi)。
ServiceDescription.Read 方法 (Stream) 通過(guò)直接從 Stream實(shí)例加載 XML 來(lái)初始化ServiceDescription類(lèi)的實(shí)例。
【3】 ServiceDescriptionImporter 類(lèi) 公開(kāi)一種為 XML Web services 生成客戶端代理類(lèi)的方法。
ServiceDescriptionImporter.AddServiceDescription 方法將指定的ServiceDescription添加到要導(dǎo)入的ServiceDescriptions值的集合中。
【4】 CodeNamespace表示命名空間聲明。
【5】 CodeCompileUnit會(huì)提供一個(gè)CodeDOM程式圓形的容器,CodeCompileUnit含有一個(gè)集合,可以?xún)?chǔ)存含有CodeDOM原始程式碼原形,專(zhuān)案參考的組件集合以及專(zhuān)案組件屬性集合的CodeNamespace物件。
【6】 CSharpCodeProvider類(lèi)提供存取C#程式碼產(chǎn)生器和程式碼編譯器的執(zhí)行個(gè)體。
【7】 取得C#程式碼編譯器的執(zhí)行個(gè)體
【8】 創(chuàng)建編譯器的參數(shù)實(shí)例
【9】 CompilerResults表示從編譯器返回的編譯結(jié)果。使用指定的編譯器設(shè)定,根據(jù)CodeCompileUnit物件之指定陣列所包含的System.CodeDom樹(shù)狀結(jié)構(gòu),編譯一個(gè)組件。
【10】 Activator類(lèi)包含特定的方法,用以在本地或從遠(yuǎn)程創(chuàng)建對(duì)象類(lèi)型,或獲取對(duì)現(xiàn)有遠(yuǎn)程對(duì)象的引用。無(wú)法繼承此類(lèi)Activator.CreateInstance 方法 使用與指定參數(shù)匹配程度最高的構(gòu)造函數(shù)創(chuàng)建指定類(lèi)型的實(shí)例。
【11】 MethodInfo 的實(shí)例可以通過(guò)調(diào)用GetMethods或者Type對(duì)象或派生自Type的對(duì)象的GetMethod方法來(lái)獲取,還可以通過(guò)調(diào)用表示泛型方法定義的 MethodInfo 的MakeGenericMethod方法來(lái)獲取。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文名稱(chēng):動(dòng)態(tài)調(diào)用webservice-創(chuàng)新互聯(lián)
URL分享:http://sd-ha.com/article38/dpcppp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、軟件開(kāi)發(fā)、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容