午夜视频免费看_日韩三级电影网站_国产精品久久一级_亚洲一级在线播放_人妻体内射精一区二区三区_91夜夜揉人人捏人人添红杏_91福利在线导航_国产又粗又猛又黄又爽无遮挡_欧美日韩一区在线播放_中文字幕一区二区三区四区不卡 _日日夜夜精品视频免费观看_欧美韩日一区二区三区

主頁 > 知識庫 > 深入.net調用webservice的總結分析

深入.net調用webservice的總結分析

熱門標簽:電銷語音自動機器人 長春呼叫中心外呼系統哪家好 鄭州400電話辦理 聯通 萊蕪外呼電銷機器人價格 凱立德導航官網地圖標注 戶外地圖標注軟件手機哪個好用 地圖標注和認領 五常地圖標注 智能電話營銷外呼系統
最近做一個項目,由于是在別人框架里開發app,導致了很多限制,其中一個就是不能直接引用webservice 。
我們都知道,調用webserivice 最簡單的方法就是在 "引用"  那里點擊右鍵,然后選擇"引用web服務",再輸入服務地址。
確定后,會生成一個app.config 里面就會自動生成了一些配置信息。
現在正在做的這個項目就不能這么干。后來經過一番搜索,就找出另外幾種動態調用webservice 的方法。
廢話少說,下面是webservice 代碼
復制代碼 代碼如下:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace TestWebService
{
    /// summary>
    /// Service1 的摘要說明
    /// /summary>
    [WebService(Namespace = "http://tempuri.org/",Description="我的Web服務")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
    // [System.Web.Script.Services.ScriptService]
    public class TestWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "測試Hello World";
        }
        [WebMethod]
        public string Test()
        {
            return "測試Test";
        }

        [WebMethod(CacheDuration = 60,Description = "測試")]
        public ListString> GetPersons()
        {
            ListString> list = new Liststring>();
            list.Add("測試一");
            list.Add("測試二");
            list.Add("測試三");
            return list;
        } 
    }
}

動態調用示例:
方法一:
看到很多動態調用WebService都只是動態調用地址而已,下面發一個不光是根據地址調用,方法名也可以自己指定的,主要原理是根據指定的WebService地址的WSDL,然后解析模擬生成一個代理類,通過反射調用里面的方法
復制代碼 代碼如下:

View Code
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Net;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Web.Services;
using System.Text;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Windows.Forms;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            String url = "http://localhost:3182/Service1.asmx?WSDL";//這個地址可以寫在Config文件里面,這里取出來就行了.在原地址后面加上: ?WSDL
            Stream stream = client.OpenRead(url);
            ServiceDescription description = ServiceDescription.Read(stream);
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//創建客戶端代理代理類。
            importer.ProtocolName = "Soap"; //指定訪問協議。
            importer.Style = ServiceDescriptionImportStyle.Client; //生成客戶端代理。
            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
            importer.AddServiceDescription(description, null, null); //添加WSDL文檔。
            CodeNamespace nmspace = new CodeNamespace(); //命名空間
            nmspace.Name = "TestWebService";
            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 = "MyTest.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");
            CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
            if (result.Errors.HasErrors)
            {
                // 顯示編譯錯誤信息
            }
            Assembly asm = Assembly.LoadFrom("MyTest.dll");//加載前面生成的程序集
            Type t = asm.GetType("TestWebService.TestWebService");
            object o = Activator.CreateInstance(t);
            MethodInfo method = t.GetMethod("GetPersons");//GetPersons是服務端的方法名稱,你想調用服務端的什么方法都可以在這里改,最好封裝一下
            String[] item = (String[])method.Invoke(o, null);
            //注:method.Invoke(o, null)返回的是一個Object,如果你服務端返回的是DataSet,這里也是用(DataSet)method.Invoke(o, null)轉一下就行了,method.Invoke(0,null)這里的null可以傳調用方法需要的參數,string[]形式的
            foreach (string str in item)
                Console.WriteLine(str);
            //上面是根據WebService地址,模似生成一個代理類,如果你想看看生成的代碼文件是什么樣子,可以用以下代碼保存下來,默認是保存在bin目錄下面
            TextWriter writer = File.CreateText("MyTest.cs");
            provider.GenerateCodeFromCompileUnit(unit, writer, null);
            writer.Flush();
            writer.Close();
        }
    }
}

方法二:利用 wsdl.exe生成webservice代理類:
根據提供的wsdl生成webservice代理類,然后在代碼里引用這個類文件。
步驟:
1、在開始菜單找到  Microsoft Visual Studio 2010 下面的Visual Studio Tools , 點擊Visual Studio 命令提示(2010),打開命令行。
2、 在命令行中輸入:  wsdl /language:c# /n:TestDemo /out:d:/Temp/TestService.cs http://jm1.services.gmcc.net/ad/Services/AD.asmx?wsdl
這句命令行的意思是:對最后面的服務地址進行編譯,在D盤temp 目錄下生成testservice文件。
3、 把上面命令編譯后的cs文件,復制到我們項目中,在項目代碼中可以直接new 一個出來后,可以進行調用。
貼出由命令行編譯出來的代碼:
復制代碼 代碼如下:

View Code
//------------------------------------------------------------------------------
// auto-generated>
//     此代碼由工具生成。
//     運行時版本:4.0.30319.225
//
//     對此文件的更改可能會導致不正確的行為,并且如果
//     重新生成代碼,這些更改將會丟失。
// /auto-generated>
//------------------------------------------------------------------------------
//
// 此源代碼由 wsdl 自動生成, Version=4.0.30319.1。
//
namespace Bingosoft.Module.SurveyQuestionnaire.DAL {
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.ComponentModel;
    using System.Web.Services.Protocols;
    using System.Web.Services;
    using System.Data;

   
    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="WebserviceForILookSoap", Namespace="http://tempuri.org/")]
    public partial class WebserviceForILook : System.Web.Services.Protocols.SoapHttpClientProtocol {

        private System.Threading.SendOrPostCallback GetRecordNumOperationCompleted;

        private System.Threading.SendOrPostCallback GetVoteListOperationCompleted;

        private System.Threading.SendOrPostCallback VoteOperationCompleted;

        private System.Threading.SendOrPostCallback GiveUpOperationCompleted;

        private System.Threading.SendOrPostCallback GetQuestionTaskListOperationCompleted;

        /// remarks/>
        public WebserviceForILook() {
            this.Url = "http://st1.services.gmcc.net/qnaire/Services/WebserviceForILook.asmx";
        }

        /// remarks/>
        public event GetRecordNumCompletedEventHandler GetRecordNumCompleted;

        /// remarks/>
        public event GetVoteListCompletedEventHandler GetVoteListCompleted;

        /// remarks/>
        public event VoteCompletedEventHandler VoteCompleted;

        /// remarks/>
        public event GiveUpCompletedEventHandler GiveUpCompleted;

        /// remarks/>
        public event GetQuestionTaskListCompletedEventHandler GetQuestionTaskListCompleted;

        /// remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetRecordNum", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public int[] GetRecordNum(string appcode, string userID) {
            object[] results = this.Invoke("GetRecordNum", new object[] {
                        appcode,
                        userID});
            return ((int[])(results[0]));
        }

        /// remarks/>
        public System.IAsyncResult BeginGetRecordNum(string appcode, string userID, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("GetRecordNum", new object[] {
                        appcode,
                        userID}, callback, asyncState);
        }

        /// remarks/>
        public int[] EndGetRecordNum(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((int[])(results[0]));
        }

        /// remarks/>
        public void GetRecordNumAsync(string appcode, string userID) {
            this.GetRecordNumAsync(appcode, userID, null);
        }

        /// remarks/>
        public void GetRecordNumAsync(string appcode, string userID, object userState) {
            if ((this.GetRecordNumOperationCompleted == null)) {
                this.GetRecordNumOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetRecordNumOperationCompleted);
            }
            this.InvokeAsync("GetRecordNum", new object[] {
                        appcode,
                        userID}, this.GetRecordNumOperationCompleted, userState);
        }

        private void OnGetRecordNumOperationCompleted(object arg) {
            if ((this.GetRecordNumCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.GetRecordNumCompleted(this, new GetRecordNumCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }

        /// remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetVoteList", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public System.Data.DataSet GetVoteList(string appcode, string userID) {
            object[] results = this.Invoke("GetVoteList", new object[] {
                        appcode,
                        userID});
            return ((System.Data.DataSet)(results[0]));
        }

        /// remarks/>
        public System.IAsyncResult BeginGetVoteList(string appcode, string userID, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("GetVoteList", new object[] {
                        appcode,
                        userID}, callback, asyncState);
        }

        /// remarks/>
        public System.Data.DataSet EndGetVoteList(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((System.Data.DataSet)(results[0]));
        }

        /// remarks/>
        public void GetVoteListAsync(string appcode, string userID) {
            this.GetVoteListAsync(appcode, userID, null);
        }

        /// remarks/>
        public void GetVoteListAsync(string appcode, string userID, object userState) {
            if ((this.GetVoteListOperationCompleted == null)) {
                this.GetVoteListOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetVoteListOperationCompleted);
            }
            this.InvokeAsync("GetVoteList", new object[] {
                        appcode,
                        userID}, this.GetVoteListOperationCompleted, userState);
        }

        private void OnGetVoteListOperationCompleted(object arg) {
            if ((this.GetVoteListCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.GetVoteListCompleted(this, new GetVoteListCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }

        /// remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Vote", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public bool Vote(string appcode, string userID, string qTaskID, string answer) {
            object[] results = this.Invoke("Vote", new object[] {
                        appcode,
                        userID,
                        qTaskID,
                        answer});
            return ((bool)(results[0]));
        }

        /// remarks/>
        public System.IAsyncResult BeginVote(string appcode, string userID, string qTaskID, string answer, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("Vote", new object[] {
                        appcode,
                        userID,
                        qTaskID,
                        answer}, callback, asyncState);
        }

        /// remarks/>
        public bool EndVote(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((bool)(results[0]));
        }

        /// remarks/>
        public void VoteAsync(string appcode, string userID, string qTaskID, string answer) {
            this.VoteAsync(appcode, userID, qTaskID, answer, null);
        }

        /// remarks/>
        public void VoteAsync(string appcode, string userID, string qTaskID, string answer, object userState) {
            if ((this.VoteOperationCompleted == null)) {
                this.VoteOperationCompleted = new System.Threading.SendOrPostCallback(this.OnVoteOperationCompleted);
            }
            this.InvokeAsync("Vote", new object[] {
                        appcode,
                        userID,
                        qTaskID,
                        answer}, this.VoteOperationCompleted, userState);
        }

        private void OnVoteOperationCompleted(object arg) {
            if ((this.VoteCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.VoteCompleted(this, new VoteCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }

        /// remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GiveUp", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public bool GiveUp(string appcode, string userID, string qTaskID) {
            object[] results = this.Invoke("GiveUp", new object[] {
                        appcode,
                        userID,
                        qTaskID});
            return ((bool)(results[0]));
        }

        /// remarks/>
        public System.IAsyncResult BeginGiveUp(string appcode, string userID, string qTaskID, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("GiveUp", new object[] {
                        appcode,
                        userID,
                        qTaskID}, callback, asyncState);
        }

        /// remarks/>
        public bool EndGiveUp(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((bool)(results[0]));
        }

        /// remarks/>
        public void GiveUpAsync(string appcode, string userID, string qTaskID) {
            this.GiveUpAsync(appcode, userID, qTaskID, null);
        }

        /// remarks/>
        public void GiveUpAsync(string appcode, string userID, string qTaskID, object userState) {
            if ((this.GiveUpOperationCompleted == null)) {
                this.GiveUpOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGiveUpOperationCompleted);
            }
            this.InvokeAsync("GiveUp", new object[] {
                        appcode,
                        userID,
                        qTaskID}, this.GiveUpOperationCompleted, userState);
        }

        private void OnGiveUpOperationCompleted(object arg) {
            if ((this.GiveUpCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.GiveUpCompleted(this, new GiveUpCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }

        /// remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetQuestionTaskList", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public System.Data.DataSet GetQuestionTaskList(string appcode, string userID) {
            object[] results = this.Invoke("GetQuestionTaskList", new object[] {
                        appcode,
                        userID});
            return ((System.Data.DataSet)(results[0]));
        }

        /// remarks/>
        public System.IAsyncResult BeginGetQuestionTaskList(string appcode, string userID, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("GetQuestionTaskList", new object[] {
                        appcode,
                        userID}, callback, asyncState);
        }

        /// remarks/>
        public System.Data.DataSet EndGetQuestionTaskList(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((System.Data.DataSet)(results[0]));
        }

        /// remarks/>
        public void GetQuestionTaskListAsync(string appcode, string userID) {
            this.GetQuestionTaskListAsync(appcode, userID, null);
        }

        /// remarks/>
        public void GetQuestionTaskListAsync(string appcode, string userID, object userState) {
            if ((this.GetQuestionTaskListOperationCompleted == null)) {
                this.GetQuestionTaskListOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetQuestionTaskListOperationCompleted);
            }
            this.InvokeAsync("GetQuestionTaskList", new object[] {
                        appcode,
                        userID}, this.GetQuestionTaskListOperationCompleted, userState);
        }

        private void OnGetQuestionTaskListOperationCompleted(object arg) {
            if ((this.GetQuestionTaskListCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.GetQuestionTaskListCompleted(this, new GetQuestionTaskListCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }

        /// remarks/>
        public new void CancelAsync(object userState) {
            base.CancelAsync(userState);
        }
    }

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    public delegate void GetRecordNumCompletedEventHandler(object sender, GetRecordNumCompletedEventArgs e);

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class GetRecordNumCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

        private object[] results;

        internal GetRecordNumCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
                base(exception, cancelled, userState) {
            this.results = results;
        }

        /// remarks/>
        public int[] Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((int[])(this.results[0]));
            }
        }
    }

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    public delegate void GetVoteListCompletedEventHandler(object sender, GetVoteListCompletedEventArgs e);

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class GetVoteListCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

        private object[] results;

        internal GetVoteListCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
                base(exception, cancelled, userState) {
            this.results = results;
        }

        /// remarks/>
        public System.Data.DataSet Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((System.Data.DataSet)(this.results[0]));
            }
        }
    }

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    public delegate void VoteCompletedEventHandler(object sender, VoteCompletedEventArgs e);

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class VoteCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

        private object[] results;

        internal VoteCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
                base(exception, cancelled, userState) {
            this.results = results;
        }

        /// remarks/>
        public bool Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((bool)(this.results[0]));
            }
        }
    }

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    public delegate void GiveUpCompletedEventHandler(object sender, GiveUpCompletedEventArgs e);

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class GiveUpCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

        private object[] results;

        internal GiveUpCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
                base(exception, cancelled, userState) {
            this.results = results;
        }

        /// remarks/>
        public bool Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((bool)(this.results[0]));
            }
        }
    }

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    public delegate void GetQuestionTaskListCompletedEventHandler(object sender, GetQuestionTaskListCompletedEventArgs e);

    /// remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class GetQuestionTaskListCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

        private object[] results;

        internal GetQuestionTaskListCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
                base(exception, cancelled, userState) {
            this.results = results;
        }

        /// remarks/>
        public System.Data.DataSet Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((System.Data.DataSet)(this.results[0]));
            }
        }
    }
}

方法三:利用http 協議的get和post
這是最為靈活的方法。
復制代碼 代碼如下:

View Code
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Bingosoft.RIA.Common
{
    /// summary>
    ///  利用WebRequest/WebResponse進行WebService調用的類
    /// /summary>
    public class WebServiceCaller
    {
        #region Tip:使用說明
        //webServices 應該支持Get和Post調用,在web.config應該增加以下代碼
        //webServices>
        //  protocols>
        //    add name="HttpGet"/>
        //    add name="HttpPost"/>
        //  /protocols>
        ///webServices>
        //調用示例:
        //Hashtable ht = new Hashtable();  //Hashtable 為webservice所需要的參數集
        //ht.Add("str", "test");
        //ht.Add("b", "true");
        //XmlDocument xx = WebSvcCaller.QuerySoapWebService("http://localhost:81/service.asmx", "HelloWorld", ht);
        //MessageBox.Show(xx.OuterXml);
        #endregion
        /// summary>
        /// 需要WebService支持Post調用
        /// /summary>
        public static XmlDocument QueryPostWebService(String URL, String MethodName, Hashtable Pars)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            byte[] data = EncodePars(Pars);
            WriteRequestData(request, data);
            return ReadXmlResponse(request.GetResponse());
        }
        /// summary>
        /// 需要WebService支持Get調用
        /// /summary>
        public static XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            return ReadXmlResponse(request.GetResponse());
        }
        /// summary>
        /// 通用WebService調用(Soap),參數Pars為String類型的參數名、參數值
        /// /summary>
        public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)
        {
            if (_xmlNamespaces.ContainsKey(URL))
            {
                return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());
            }
            else
            {
                return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));
            }
        }
        private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs)
        {
            _xmlNamespaces[URL] = XmlNs;//加入緩存,提高效率
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "text/xml; charset=utf-8";
            request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
            SetWebRequest(request);
            byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName);
            WriteRequestData(request, data);
            XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
            doc = ReadXmlResponse(request.GetResponse());
            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            String RetXml = doc.SelectSingleNode("http://soap:Body/*/*", mgr).InnerXml;
            doc2.LoadXml("root>" + RetXml + "/root>");
            AddDelaration(doc2);
            return doc2;
        }
        private static string GetNamespace(String URL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
            SetWebRequest(request);
            WebResponse response = request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sr.ReadToEnd());
            sr.Close();
            return doc.SelectSingleNode("http://@targetNamespace").Value;
        }
        private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">/soap:Envelope>");
            AddDelaration(doc);
            //XmlElement soapBody = doc.createElement_x_x("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
            XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
            //XmlElement soapMethod = doc.createElement_x_x(MethodName);
            XmlElement soapMethod = doc.CreateElement(MethodName);
            soapMethod.SetAttribute("xmlns", XmlNs);
            foreach (string k in Pars.Keys)
            {
                //XmlElement soapPar = doc.createElement_x_x(k);
                XmlElement soapPar = doc.CreateElement(k);
                soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
                soapMethod.AppendChild(soapPar);
            }
            soapBody.AppendChild(soapMethod);
            doc.DocumentElement.AppendChild(soapBody);
            return Encoding.UTF8.GetBytes(doc.OuterXml);
        }
        private static string ObjectToSoapXml(object o)
        {
            XmlSerializer mySerializer = new XmlSerializer(o.GetType());
            MemoryStream ms = new MemoryStream();
            mySerializer.Serialize(ms, o);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
            if (doc.DocumentElement != null)
            {
                return doc.DocumentElement.InnerXml;
            }
            else
            {
                return o.ToString();
            }
        }
        /// summary>
        /// 設置憑證與超時時間
        /// /summary>
        /// param name="request">/param>
        private static void SetWebRequest(HttpWebRequest request)
        {
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 10000;
        }
        private static void WriteRequestData(HttpWebRequest request, byte[] data)
        {
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
        }
        private static byte[] EncodePars(Hashtable Pars)
        {
            return Encoding.UTF8.GetBytes(ParsToString(Pars));
        }
        private static String ParsToString(Hashtable Pars)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in Pars.Keys)
            {
                if (sb.Length > 0)
                {
                    sb.Append("");
                }
                //sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
            }
            return sb.ToString();
        }
        private static XmlDocument ReadXmlResponse(WebResponse response)
        {
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(retXml);
            return doc;
        }
        private static void AddDelaration(XmlDocument doc)
        {
            XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.InsertBefore(decl, doc.DocumentElement);
        }
        private static Hashtable _xmlNamespaces = new Hashtable();//緩存xmlNamespace,避免重復調用GetNamespace
    }
}

您可能感興趣的文章:
  • 淺談對Jquery+JSON+WebService的使用小結
  • 解析利用wsdl.exe生成webservice代理類的詳解
  • 在Android中訪問WebService接口的方法
  • c#動態調用Webservice的兩種方法實例
  • 利用soaplib搭建webservice詳細步驟和實例代碼
  • C#動態webservice調用接口
  • WebService教程詳解(二)

標簽:宣城 福州 湖州 西藏 衢州 西寧 岳陽 紅河

巨人網絡通訊聲明:本文標題《深入.net調用webservice的總結分析》,本文關鍵詞  深入,.net,調用,webservice,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《深入.net調用webservice的總結分析》相關的同類信息!
  • 本頁收集關于深入.net調用webservice的總結分析的相關信息資訊供網民參考!
  • 推薦文章
    国产人妻精品午夜福利免费| 亚洲精品国产精品国自产网站按摩| 亚洲高清在线播放| 在线a欧美视频| 丰满亚洲少妇av| 秋霞欧美一区二区三区视频免费| 伊人久久大香线蕉av一区| 日韩一区二区三区国产| 亚洲精品亚洲人成人网在线播放| 99国产揄拍国产精品| 18禁一区二区三区| 精品网站在线看| 亚洲香蕉成人av网站在线观看 | 在线观看免费av网址| 成人激情视频在线播放| 精品88久久久久88久久久| 久久久五月婷婷| 一区二区视频免费观看| av免费观看不卡| 欧美日韩国产免费一区二区三区| 精品国产美女在线| 偷拍日韩校园综合在线| 美腿丝袜在线亚洲一区| 国产在线视频卡一卡二| 亚洲成人福利在线| 蜜桃av久久久亚洲精品| 欧美成人免费全部| 日本精品一区二区三区高清 | 欧美一区二区视频在线播放| 色爱av美腿丝袜综合粉嫩av| 日韩av影院在线观看| 亚洲精品理论电影| 日韩a∨精品日韩在线观看| 国产午夜福利片| 日韩精品成人一区二区在线| 国产精品蜜臀在线观看| 久久综合色8888| 最好看的2019年中文视频| 最新中文字幕久久| 美女网站色91| 97精品国产97久久久久久免费| www.av免费| 国产女人aaa级久久久级| 福利视频久久| 性囗交免费视频观看| 91中文字幕在线视频| av成人免费在线| 日韩欧美国产精品一区| 欧美日韩精品久久久免费观看| 欧美一级中文字幕| 夜夜春很很躁夜夜躁| 久久久亚洲精品一区二区三区| 国产精品视频在线播放| 国产制服丝袜在线| 久久精品国产第一区二区三区| 欧美日韩综合在线| 欧美无砖砖区免费| 日本一区二区三区免费观看| 99热6这里只有精品| 欧美日韩免费看| 少妇黄色一级片| 亚洲人成小说网站色在线| 日韩久久久久久久久久久久| 国产一区二区成人久久免费影院 | 中文字字幕在线观看| 久久伊人中文字幕| 色综合久久久久久中文网| 日韩精品第1页| 中文在线观看免费视频| 国产av一区二区三区精品| 在线电影国产精品| 黄色片子免费看| 午夜久久久久久久久久| 色综合欧美在线视频区| 最新精品视频| 91精品视频免费| 亚洲专区区免费| 三级欧美韩日大片在线看| 国产精品亚洲成人| 欧美性xxxx在线播放| 成人免费视频网站| 久久久精品视频网站 | 亚洲熟妇一区二区| av一区二区三区| 韩国一区二区电影| 日本r级电影在线观看| 好吊色一区二区三区| 色综合天天综合给合国产| 亚洲a∨日韩av高清在线观看| www.自拍偷拍| 天堂中文网在线| 国产亚洲va综合人人澡精品 | 久久久久久综合网| 97超碰人人澡| 亚洲视频久久久| 中文字幕精品一区久久久久 | 给我免费观看片在线电影的| 亚洲视频图片小说| 日产国产精品精品a∨| 国产伦精品一区二区三区四区| 日韩av资源在线播放| www.美色吧.com| 亚洲不卡一区二区三区| 亚洲视频sss| 三级一区在线视频先锋| 久久亚洲影音av资源网 | 5g国产欧美日韩视频| 丰满人妻一区二区三区免费视频| 亚洲一区免费网站| 国产又爽又黄免费软件| 精品国产露脸精彩对白| 亚洲va韩国va欧美va精四季| 日韩电影在线观看电影| 最近的2019中文字幕免费一页 | 亚洲国产日日夜夜| 国产成人jvid在线播放| 国产精品特级毛片一区二区三区| 久久久视频在线| 四川一级毛毛片| 99久久夜色精品国产网站| 国产精品亚洲美女av网站| av久久久久久| www.中文字幕av| 欧美日韩在线免费视频| 麻豆精品国产传媒| 色乱码一区二区三区88| 刘亦菲国产毛片bd| 欧美精品一本久久男人的天堂| 亚洲 小说区 图片区 都市| 欧美精品免费观看二区| 亚洲国产精品久久久久爰性色| 亚洲欧美国产另类| 亚洲欧美日本一区| 欧美性猛片xxxx免费看久爱| 精品久久久久久无码中文野结衣 | 亚洲高清视频一区| 国产一区二区不卡在线| www.日本少妇| 国产又黄又大久久| 国产99在线播放| 亚洲av成人精品毛片| 欧美一卡在线观看| 黄色大片在线免费看| 成人av一区二区三区| 人人妻人人做人人爽| 欧美日韩一级二级| 国产午夜精品无码一区二区| 国产精品第10页| 久久综合成人精品亚洲另类欧美 | 91视频在线看| 91亚色免费| 亚洲aaaaaaa| 97成人超碰免| 超碰在线观看av| 欧美大片国产精品| 免费看黄色一级大片| 亚洲高清视频中文字幕| 极品人妻videosss人妻| 日韩一区二区三区av| 亚洲精品在线网址| 欧美性xxxxx极品少妇| av免费观看不卡| 狂野欧美一区| 色之综合天天综合色天天棕色| 久久免费美女视频| 中文字幕一区二区三区乱码 | 日本xxxxxxxxxx75| 粉嫩一区二区三区在线看| 欧美18视频| 国产精品人人爽| 日韩精品免费在线| 国产成年人视频网站| 日韩欧美一区二区视频| 亚洲天堂avav| 91国产中文字幕| 国产99999| 成人h猎奇视频网站| 黄色美女一级片| 亚洲最新视频在线| 国产视频在线免费观看| 成人久久精品视频| 亚洲激情六月丁香| 国产剧情在线视频| 色一情一乱一伦一区二区三区丨| 欧美色综合网站| 精品人妻aV中文字幕乱码色欲| 蜜乳av一区二区三区| 久久草.com| 99国产精品久久久久| 日本wwww视频| 久久伊人蜜桃av一区二区| 亚洲国产婷婷香蕉久久久久久99| 国产在线视频一区二区| 欧美变态另类刺激| 国产丝袜精品第一页| 中文字幕人妻一区二区三区视频| 中文字幕在线日韩| 精品二区三区线观看| 亚洲第一区第二区| 天天做天天爱夜夜爽| 日本三级久久久| 国产女同性恋一区二区| www.av免费| 国产一区二区三区直播精品电影| 国产真实乱人偷精品人妻| 精品国产老师黑色丝袜高跟鞋| http;//www.99re视频| 人妻一区二区三区| 国产精品老女人视频| 日本精品久久久久久| 伊人久久大香线蕉成人综合网| 岛国av一区二区| 日韩三级久久久| 国产成人免费av| 五月婷婷综合在线| 成 人 免费 黄 色| 中文字幕在线看高清电影| 久久精品日韩| 亚洲精品福利在线观看| 成年人国产精品| 香蕉视频xxxx| 综合网中文字幕| 在线免费a视频| 成人3d动漫一区二区三区91| 中文字幕免费在线观看视频一区| 色噜噜狠狠一区二区三区狼国成人| 中文字幕欧美精品在线 | 欧美一区二区三区艳史| 久久久999久久久| 欧美一级电影免费在线观看| 香蕉av在线播放| 一卡二卡3卡四卡高清精品视频| 亚洲国产欧美一区二区三区同亚洲 | 怡红院成永久免费人全部视频| 日韩午夜视频在线观看| 成人精品gif动图一区| 男人女人拔萝卜视频| 日韩视频免费看| 日韩黄色免费电影| www.爱色av.com| 亚洲男人天堂九九视频| 天堂影院一区二区| 成人精品视频一区二区| 中文字幕视频在线免费欧美日韩综合在线看 | 精品久久久网站| 日韩福利片在线观看| 国产在线资源一区| 欧美成人性福生活免费看| 天天综合网在线| 久久人人爽人人爽人人av| 一区二区三区动漫| 日本一区二区三区四区五区| 天天爽天天狠久久久| 日韩亚洲电影在线| 成人黄色在线观看视频| 欧美黑人一级爽快片淫片高清| 紧缚奴在线一区二区三区| 久草综合在线观看| 免费成人高清视频| 久久女同互慰一区二区三区| 人体私拍套图hdxxxx| 国产午夜精品久久久| 中文字幕日产av| 欧美精品亚洲精品| 亚洲成人777| 国产精品视频一区二区三| 久久久久久久久一区| 精品国产免费视频| 久久久久久一二三区| 国产特级黄色录像| 鲁片一区二区三区| 日韩精品www| 香蕉av福利精品导航| 少妇一级淫片免费放中国| 欧美另类z0zx974| 成人免费在线小视频| 97人人干人人| 日韩欧美国产午夜精品| 亚洲人成在线观看一区二区| 伊人久久亚洲综合| 性高潮免费视频| 精品欧美一区二区三区久久久 | 久久久久久一区二区三区| 一区二区三区日韩欧美| 国产精品久久久久久在线| 中文字幕第66页| 狠狠久久综合婷婷不卡| 欧美美女网站色| 熟妇高潮一区二区三区| 免费观看一区二区三区| 国产综合在线观看视频| 欧美猛男男办公室激情| 国产a精品视频| 国产精品自拍视频一区| 国产一区二区高清不卡| 精品久久中文字幕久久av| 自拍偷拍福利视频| 久热免费在线观看| 久久91亚洲人成电影网站| 国产女主播一区| 久久精品国语| 福利一区二区三区四区| 日本成年人网址| 日韩免费在线看| 精品精品国产高清一毛片一天堂| 日本一区二区三区dvd视频在线 | 亚洲国产精品国自产拍久久| www国产黄色| **欧美日韩vr在线| 一区二区三区国产豹纹内裤在线| 免费看毛片网站| 熟妇高潮一区二区| 亚洲欧美国产一区二区| 久久久久久国产| 91黄色小视频| 国产日韩成人精品| 日韩精品乱码免费| 国产乱码精品一区二区三区精东| www.国产色| 国产一级特黄毛片| 国产又黄又猛又粗又爽| 国产盗摄一区二区三区在线| 黄色性视频网站| 无码人妻aⅴ一区二区三区日本| 国产精品国产亚洲伊人久久| 亚洲精品动漫100p| 国产精品免费观看视频| 免费激情视频网站| 好吊日在线视频| 欧美综合在线观看视频| 日韩久久在线| 亚洲xxxxx| 欧美mv日韩mv亚洲| 国产亚洲欧美激情| 精品国产亚洲av麻豆| 中国极品少妇videossexhd| 一区二区在线观| 日韩免费观看网站| 色综合久久悠悠| 精品无码久久久久久国产| 欧美日韩二区三区| 成人黄色av电影| 蜜臀av性久久久久蜜臀aⅴ流畅| 黄色一级视频免费看| 在线观看成人毛片| 亚洲国产综合久久| 久久亚洲天堂网| 精品久久久久久亚洲综合网站| 99热这里只有精品1| 亚洲AV无码一区二区三区性 | 日韩三级电影网址| 国产午夜精品理论片a级大结局| 国产专区欧美精品| 国产一区二区三区在线看麻豆| 天天摸天天碰天天爽天天弄| 豆国产97在线 | 亚洲| 日本在线观看免费视频| 日韩国产欧美亚洲| 99热自拍偷拍| 一区二区三区免费看| 青青草视频在线免费播放 | 黄色国产精品视频| 性活交片大全免费看| 欧美日韩中文字幕视频| 国产女人18水真多毛片18精品| 欧美成人一二三区| 日韩a在线播放| av天堂永久资源网| 久草在在线视频| 国产无套粉嫩白浆内谢的出处| 免费看成人午夜电影| 九九九九久久久久| 久久99蜜桃综合影院免费观看| 亚洲精品免费av| 久久99国产精品| 日本精品二区| 日韩av在线一区二区三区| 国产欧美日韩中文| 亚州欧美日韩中文视频| xxxx性欧美| 国产午夜精品视频免费不卡69堂| 91精品国产欧美一区二区18 | 中国一级特黄视频| 中文字幕二区三区| 影音先锋亚洲天堂| 日韩在线视频不卡| 人妻无码中文字幕| 少妇荡乳情欲办公室456视频| 成人激情动漫在线观看| 五月天久久比比资源色| 日韩成人中文字幕| 97在线视频免费| 国产欧美精品一区二区三区| 一本大道熟女人妻中文字幕在线| 天天操天天摸天天舔| 91 中文字幕| 99精品久久只有精品| 一区二区三区视频在线看| 欧美日韩一级视频| 欧美一二三区精品| 麻豆成人在线看| 久久九九国产精品怡红院 | 九色在线视频观看| 日韩精品免费播放| 成人做爰www看视频软件 | 精品国产黄色片| 熟妇高潮一区二区高潮| 激情深爱一区二区| 一区二区不卡在线播放| 日韩高清欧美高清|