Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / Java

How to Use JS Calling A WebService By Post Both SOAP And JSON

Rate me:
Please Sign up or sign in to vote.
3.29/5 (6 votes)
8 Apr 2009CPOL2 min read 72.9K   306   18  
WebService Both SOAP AND JSON
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

using System.Runtime.Serialization;
using System.IO;
using System.Web;

namespace kuuy.Components.Services
{
    //WebService扩展
    public class EnhancedWebService : System.Web.Services.WebService
    {
        public EnhancedWebService()
            : base()//调用基类构造
        {
            string ServiceMethodName = GetMethodName();
            bool IsJSON = Context.Request.QueryString["out"] == "json";
            if (IsJSON) InterceptJSONMethodRequest(ServiceMethodName);
        }
        private string GetMethodName()//获得方法名
        {
            return Context.Request.Url.Segments[Context.Request.Url.Segments.Length - 1];//URL段数(/)
        } 
        private void InterceptJSONMethodRequest(string ServiceMethodName)
        {
            JSONMethodAttribute JMA = GetMethodJSONMethodAttribute(ServiceMethodName);//描述
            if (JMA == null)
            {
                Context.Response.Write("throw new Exception('The Web Service method " +
                                       ServiceMethodName + " is not available " +
                                       "as a JSON function. For more " +
                                       "information contact " +
                                       "the Web Service Administrators.');");
            }
            else
            {
                //ToDo: deserialize parameters, call target method, 
                //      deserialize and write return value
                Type Service = this.GetType();
                MethodInfo JSONMethod = Service.GetMethod(ServiceMethodName);//获取构造函数的参数
                if (JSONMethod == null) return;
                //参数集
                ParameterInfo[] JSONMethodParameters = JSONMethod.GetParameters();
                object[] CallParameters = new object[JSONMethodParameters.Length];
                //遍历
                for (int i = 0; i < JSONMethodParameters.Length; i++)
                {
                    //参数
                    ParameterInfo TargetParameter = JSONMethodParameters[i];
                    string RawParameter = HttpUtility.UrlDecode(Context.Request.Form[TargetParameter.Name]);//获取值
                    if (RawParameter == null || RawParameter == "")
                        throw new Exception("Missing parameter " + TargetParameter.Name + ".");
                    CallParameters[i] = JSONHelper.Deserialize<object>(RawParameter, TargetParameter.ParameterType);
                }
                object JSONMethodReturnValue = JSONMethod.Invoke(this, CallParameters);
                string SerializedReturnValue = JSONHelper.Serialize<object>(JSONMethodReturnValue);//返回JSON串
                Context.Response.Write(SerializedReturnValue);
            }
            Context.Response.Flush();
            Context.Response.End();
        }
        private JSONMethodAttribute GetMethodJSONMethodAttribute(string WebServiceMethodName)
        {
            MethodInfo ActiveMethod = this.GetType().GetMethod(WebServiceMethodName);
            JSONMethodAttribute JMA = null;
            if (ActiveMethod != null)
            {
                object[] Attributes = ActiveMethod.GetCustomAttributes(true);
                foreach (object Attribute in Attributes)
                {
                    if (Attribute.GetType() == typeof(JSONMethodAttribute))
                    {
                        JMA = (JSONMethodAttribute)Attribute;
                    }
                }
            }
            return JMA;
        }
    }
    //JSON描述类
    public class JSONMethodAttribute : System.Attribute
    {
        public JSONMethodAttribute()
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions