c#调用webservices_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > c#调用webservices

c#调用webservices

 2013/7/17 17:14:47  沧海一滴  博客园  我要评论(0)
  • 摘要:有两种方式,静态调用(添加web服务的暂且这样定义)和动态调用:静态调用:使用添加web服务的方式支持各种参数,由于vs2010会自动转换,会生成一个特定的Reference.cs类文件 动态调用:通过soap/get/post方法动态调用webservices,只支持string类型或数组类型否则执行XmlSerializermySerializer=newXmlSerializer(o.GetType());会报错:不支持类型System.Collections
  • 标签:C# Web Service Webservice


有两种方式,静态调用(添加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:

class="alt">//------------------------------------------------------------------------------
// <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());

发表评论
用户名: 匿名