c#調用webservices



有兩種方式,靜態調用(添加web服務的暫且這樣定義)和動態調用:

靜態調用:
使用添加web服務的方式支持各種參數,由於vs2010會自動轉換,會生成一個特定的Reference.cs類文件

 

動態調用:
通過soap/get/post方法動態調用webservices,只支持string類型或數組類型

否則執行

XmlSerializer mySerializer = new XmlSerializer(o.GetType());

會報錯:
不支持類型 System.Collections.Hashtable,因為它實現 IDictionary。

說明:靜態調用也是將其它數據類型轉換成Object[]類型的數組來傳參
譬如java中HashMap類型,vs2010轉換后的情況如下:

Reference.cs:

//------------------------------------------------------------------------------
// <auto-generated>
//     此代碼由工具生成。
//     運行時版本:4.0.30319.1008
//
//     對此文件的更改可能會導致不正確的行為,並且如果
//     重新生成代碼,這些更改將會丟失。
// </auto-generated>
//------------------------------------------------------------------------------
 
// 
// 此源代碼是由 Microsoft.VSDesigner 4.0.30319.1008 版自動生成。
// 
#pragma warning disable 1591
 
namespace WinFormFunction.localhost {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.ComponentModel;
    using System.Xml.Serialization;
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="CreateGevtIncidentServiceHttpBinding", Namespace="http://alarm.mobile.work.inter.bomc.boco.com")]
    public partial class CreateGevtIncidentService : System.Web.Services.Protocols.SoapHttpClientProtocol {
        
        private System.Threading.SendOrPostCallback createIncidentOperationCompleted;
        
        private bool useDefaultCredentialsSetExplicitly;
        
        /// <remarks/>
        public CreateGevtIncidentService() {
            this.Url = global::WinFormFunction.Properties.Settings.Default.WinFormFunction_localhost_CreateGevtIncidentService;
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }
        
        public new string Url {
            get {
                return base.Url;
            }
            set {
                if ((((this.IsLocalFileSystemWebService(base.Url) == true) 
                            && (this.useDefaultCredentialsSetExplicitly == false)) 
                            && (this.IsLocalFileSystemWebService(value) == false))) {
                    base.UseDefaultCredentials = false;
                }
                base.Url = value;
            }
        }
        
        public new bool UseDefaultCredentials {
            get {
                return base.UseDefaultCredentials;
            }
            set {
                base.UseDefaultCredentials = value;
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }
        
        /// <remarks/>
        public event createIncidentCompletedEventHandler createIncidentCompleted;
        
        /// <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace="http://alarm.mobile.work.inter.bomc.boco.com", ResponseNamespace="http://alarm.mobile.work.inter.bomc.boco.com", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlArrayAttribute("out", IsNullable=true)]
        [return: System.Xml.Serialization.XmlArrayItemAttribute("entry", IsNullable=false)]
        public anyType2anyTypeMapEntry[] createIncident([System.Xml.Serialization.XmlArrayAttribute(IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("entry", IsNullable=false)] anyType2anyTypeMapEntry[] map) {
            object[] results = this.Invoke("createIncident", new object[] {
                        map});
            return ((anyType2anyTypeMapEntry[])(results[0]));
        }
        
        /// <remarks/>
        public void createIncidentAsync(anyType2anyTypeMapEntry[] map) {
            this.createIncidentAsync(map, null);
        }
        
        /// <remarks/>
        public void createIncidentAsync(anyType2anyTypeMapEntry[] map, object userState) {
            if ((this.createIncidentOperationCompleted == null)) {
                this.createIncidentOperationCompleted = new System.Threading.SendOrPostCallback(this.OncreateIncidentOperationCompleted);
            }
            this.InvokeAsync("createIncident", new object[] {
                        map}, this.createIncidentOperationCompleted, userState);
        }
        
        private void OncreateIncidentOperationCompleted(object arg) {
            if ((this.createIncidentCompleted != null)) {
                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                this.createIncidentCompleted(this, new createIncidentCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
            }
        }
        
        /// <remarks/>
        public new void CancelAsync(object userState) {
            base.CancelAsync(userState);
        }
        
        private bool IsLocalFileSystemWebService(string url) {
            if (((url == null) 
                        || (url == string.Empty))) {
                return false;
            }
            System.Uri wsUri = new System.Uri(url);
            if (((wsUri.Port >= 1024) 
                        && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {
                return true;
            }
            return false;
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1009")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://alarm.mobile.work.inter.bomc.boco.com")]
    public partial class anyType2anyTypeMapEntry {
        
        private object keyField;
        
        private object valueField;
        
        /// <remarks/>
        public object key {
            get {
                return this.keyField;
            }
            set {
                this.keyField = value;
            }
        }
        
        /// <remarks/>
        public object value {
            get {
                return this.valueField;
            }
            set {
                this.valueField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.1")]
    public delegate void createIncidentCompletedEventHandler(object sender, createIncidentCompletedEventArgs e);
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class createIncidentCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        internal createIncidentCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        /// <remarks/>
        public anyType2anyTypeMapEntry[] Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((anyType2anyTypeMapEntry[])(this.results[0]));
            }
        }
    }
}
 
#pragma warning restore 1591

調用:
localhost.CreateGevtIncidentService client = new localhost.CreateGevtIncidentService();

localhost.anyType2anyTypeMapEntry atatm1 = new localhost.anyType2anyTypeMapEntry();
localhost.anyType2anyTypeMapEntry atatm2 = new localhost.anyType2anyTypeMapEntry();

atatm1.key = "title";
atatm1.value = "test title";

atatm2.key = "content";
atatm2.value = "testContent";

localhost.anyType2anyTypeMapEntry[] test=client.createIncident(new localhost.anyType2anyTypeMapEntry[] { atatm1,atatm2});
logger.Info(test.ToString());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM