c#呼叫webservices

滄海一滴發表於2013-07-17


有兩種方式,靜態呼叫(新增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());

相關文章