C#調(diào)用WebService的方法步驟
前言
在日常工作中,如果涉及到與第三方進(jìn)行接口對(duì)接,有的會(huì)使用WebService的方式,這篇文章主要講解在.NET Framework中如何調(diào)用WebService。
創(chuàng)建WebService
(1)新建項(xiàng)目——模板選擇ASP.NET Web 應(yīng)用程序
(2)選擇空項(xiàng)目模板
(3)右擊項(xiàng)目-添加-Web服務(wù)(ASMX)
(4)新建后會(huì)自動(dòng)生成一個(gè)測(cè)試服務(wù)HelloWorld并返回執(zhí)行字符串
(5)點(diǎn)擊運(yùn)行,并調(diào)用返回
方法一:靜態(tài)引用
這種方式是通過(guò)添加靜態(tài)引用的方式調(diào)用WebService
1.首先創(chuàng)建一個(gè)Winform程序,右擊引用-添加服務(wù)引用。地址即為 運(yùn)行的WebService地址,命名空間可自命名
2.設(shè)計(jì)Winform窗體,可選擇工具箱button調(diào)用,TextBox入?yún)?/p>
3.根據(jù)所需,調(diào)用WebService服務(wù)即可拿到返回參數(shù)
方法二:動(dòng)態(tài)調(diào)用
上面使用靜態(tài)引用的方式調(diào)用WebService,但是這種方式有一個(gè)缺點(diǎn):如果發(fā)布的WebService地址改變,那么就要重新添加WebService的引用。如果是現(xiàn)有的WebService發(fā)生了改變,也要更新現(xiàn)有的服務(wù)引用,這需要把代碼放到現(xiàn)場(chǎng)才可以。
使用動(dòng)態(tài)調(diào)用WebService的方法可以解決該問(wèn)題。
1.我們?cè)谂渲梦募锩嫣砑优渲?,把WebService的地址、WebService提供的類名、要調(diào)用的方法名稱,都寫在配置文件里面
2.同樣設(shè)計(jì)Winform界面,添加按鈕,調(diào)用WebService服務(wù)
添加幫助類
using System; using System.CodeDom.Compiler; using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.Web; using System.Xml.Serialization; using System.Web.Caching; using System.Web.Services.Description; namespace ApiTest1 { internal class WebServiceHelper { /// <summary> /// 生成dll文件保存到本地 /// </summary> /// <param name="url">WebService地址</param> /// <param name="className">類名</param> /// <param name="methodName">方法名</param> /// <param name="filePath">保存dll文件的路徑</param> public static void CreateWebServiceDLL(string url, string className, string methodName, string filePath) { // 1. 使用 WebClient 下載 WSDL 信息。 WebClient web = new WebClient(); Stream stream = web.OpenRead(url + "?WSDL"); // 2. 創(chuàng)建和格式化 WSDL 文檔。 ServiceDescription description = ServiceDescription.Read(stream); //如果不存在就創(chuàng)建file文件夾 if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } if (File.Exists(filePath + className + "_" + methodName + ".dll")) { //判斷緩存是否過(guò)期 var cachevalue = HttpRuntime.Cache.Get(className + "_" + methodName); if (cachevalue == null) { //緩存過(guò)期刪除dll File.Delete(filePath + className + "_" + methodName + ".dll"); } else { // 如果緩存沒(méi)有過(guò)期直接返回 return; } } // 3. 創(chuàng)建客戶端代理代理類。 ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); // 指定訪問(wèn)協(xié)議。 importer.ProtocolName = "Soap"; // 生成客戶端代理。 importer.Style = ServiceDescriptionImportStyle.Client; importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync; // 添加 WSDL 文檔。 importer.AddServiceDescription(description, null, null); // 4. 使用 CodeDom 編譯客戶端代理類。 // 為代理類添加命名空間,缺省為全局空間。 CodeNamespace nmspace = new CodeNamespace(); CodeCompileUnit unit = new CodeCompileUnit(); unit.Namespaces.Add(nmspace); ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit); CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); CompilerParameters parameter = new CompilerParameters(); parameter.GenerateExecutable = false; // 可以指定你所需的任何文件名。 parameter.OutputAssembly = filePath + className + "_" + methodName + ".dll"; parameter.ReferencedAssemblies.Add("System.dll"); parameter.ReferencedAssemblies.Add("System.XML.dll"); parameter.ReferencedAssemblies.Add("System.Web.Services.dll"); parameter.ReferencedAssemblies.Add("System.Data.dll"); // 生成dll文件,并會(huì)把WebService信息寫入到dll里面 CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit); if (result.Errors.HasErrors) { // 顯示編譯錯(cuò)誤信息 System.Text.StringBuilder sb = new StringBuilder(); foreach (CompilerError ce in result.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //記錄緩存 var objCache = HttpRuntime.Cache; // 緩存信息寫入dll文件 objCache.Insert(className + "_" + methodName, "1", null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.High, null); } /// <summary> /// 根據(jù)WebService的url地址獲取className /// </summary> /// <param name="wsUrl">WebService的url地址</param> /// <returns></returns> //private string GetWsClassName(string wsUrl) //{ // string[] parts = wsUrl.Split('/'); // string[] pps = parts[parts.Length - 1].Split('.'); // return pps[0]; //} } }
3.動(dòng)態(tài)調(diào)用WebService代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ApiTest1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 讀取配置文件,獲取配置信息 string url = ConfigurationManager.AppSettings["WebServiceAddress"]; string className = ConfigurationManager.AppSettings["ClassName"]; string methodName = ConfigurationManager.AppSettings["MethodName"]; string filePath = ConfigurationManager.AppSettings["FilePath"]; // 調(diào)用WebServiceHelper WebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath); // 讀取dll內(nèi)容 byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll"); // 加載程序集信息 Assembly asm = Assembly.Load(filedata); Type t = asm.GetType(className); // 創(chuàng)建實(shí)例 object o = Activator.CreateInstance(t); MethodInfo method = t.GetMethod(methodName); // 參數(shù) string MsgCode = textBox1.Text; string SendXml = textBox2.Text; //string UserCode = textBox3.Text; object[] args = { MsgCode, SendXml}; //object[] args = { "動(dòng)態(tài)調(diào)用WebService" }; // 調(diào)用訪問(wèn),獲取方法返回值 string value = method.Invoke(o, args).ToString(); //輸出返回值 MessageBox.Show($"返回值:{value}"); } } }
程序運(yùn)行結(jié)果
如果說(shuō)類名沒(méi)有提供,可以根據(jù)url來(lái)自動(dòng)獲取類名:
見(jiàn)幫助類(WebServiceHelper)中GetWsClassName 方法。
到此這篇關(guān)于C#調(diào)用WebService的方法步驟的文章就介紹到這了,更多相關(guān)C#調(diào)用WebService內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#編寫的高并發(fā)數(shù)據(jù)庫(kù)控制訪問(wèn)代碼
往往大數(shù)據(jù)量,高并發(fā)時(shí), 瓶頸都在數(shù)據(jù)庫(kù)上, 好多人都說(shuō)用數(shù)據(jù)庫(kù)的復(fù)制,發(fā)布, 讀寫分離等技術(shù), 但主從數(shù)據(jù)庫(kù)之間同步時(shí)間有延遲.2015-03-03C#調(diào)用海康工業(yè)相機(jī)SDK采集圖像并在Halcon窗口中顯示方式
這篇文章主要介紹了C#調(diào)用??倒I(yè)相機(jī)SDK采集圖像并在Halcon窗口中顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02WindowsForm實(shí)現(xiàn)TextBox占位符Placeholder提示功能
這篇文章主要介紹了WindowsForm實(shí)現(xiàn)TextBox占位符Placeholder提示,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07C#判斷頁(yè)面中的多個(gè)文本框輸入值是否有重復(fù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#判斷頁(yè)面中的多個(gè)文本框輸入值是否有重復(fù)的實(shí)現(xiàn)方法,是一個(gè)非常簡(jiǎn)單實(shí)用的技巧,需要的朋友可以參考下2014-10-10Unity中C#和Java的相互調(diào)用實(shí)例代碼
在unity中接入sdk或者定制一些功能時(shí),需要調(diào)用系統(tǒng)接口。安卓手機(jī)實(shí)際操作中,也就是Unity與android相互調(diào)用。我們?cè)赨nity中使用c#,android中使用java。2018-02-02WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解
這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01.NET WinForm實(shí)現(xiàn)在listview中添加progressbar的方法
這篇文章主要介紹了.NET WinForm實(shí)現(xiàn)在listview中添加progressbar的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了進(jìn)度條控件的添加與使用方法,需要的朋友可以參考下2017-05-05C#?WPF實(shí)現(xiàn)播放音頻文件的示例詳解
這篇文章主要為大家詳細(xì)介紹了利用C#?WPF實(shí)現(xiàn)播放音頻文件的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03