Beispiel #1
0
        /// <summary>
        /// 对请求数据进行包装,转换为合适的请求类型,并返回对应的负载类型
        /// </summary>
        /// <param name="data">要发送的数据</param>
        /// <param name="contentType">对应的负载类型</param>
        /// <param name="extraRequestInfo">额外的请求数据</param>
        /// <returns>经过包装的 <see cref="HttpRequestContent"/> 对象</returns>
        public virtual HttpRequestContent WrapRequestContent(object data, ContentType?contentType, ExtraRequestInfo extraRequestInfo)
        {
            if (data == null)
            {
                return(null);
            }

            var type = contentType ?? GetPreferContentType(data.GetType()) ?? Setting.DefaultRequestContentType;
            var ea   = new RequestWrapRequestContentEventArgs(this, data, extraRequestInfo)
            {
                ContentType = type
            };

            ea.RequestContent = Setting.ContentPayloadBuilder.WrapRequestContent(data, ea);

            GlobalEvents.OnBeforeRequestWrapRequestContent(this, ea);
            OnBeforeWrapRequestContent(this, ea);
            var content = ea.RequestContent;

            contentType = ea.ContentType;

            if (content == null)
            {
                if (data is HttpRequestContent)
                {
                    content = data as HttpRequestContent;
                }
                else if (data is string)
                {
                    content = new RequestStringContent(data as string, type);
                }
                else if (data is Stream)
                {
                    content = new RequestCopyStreamContent(data as Stream, type);
                }
                else if (data is byte[])
                {
                    content = new RequestByteBufferContent(data as byte[], contentType: type);
                }
                else if (data is IDictionary <string, string> && type != ContentType.Json)
                {
                    content = new RequestFormDataContent(data as IDictionary <string, string>, type);
                }
                else if (data is XmlDocument || data is XmlNode || data is System.Xml.Linq.XDocument)
                {
                    content = new RequestXmlContent(data, contentType ?? ContentType.Xml);
                }
                //object

                else if (contentType == ContentType.Json)
                {
                    content = new RequestJsonContent(data);
                }
                else if (contentType == ContentType.Xml)
                {
                    content = new RequestXmlContent(data, type);
                }
                else
                {
                    content = new RequestObjectContent <object>(data, type);
                }
            }

            //全局事件
            GlobalEvents.OnRequestWrapRequestContent(this, ea);

            //实例事件
            OnRequestWrapRequestContent(this, ea);

            content = HttpHandler.WrapRequestContent(this, content, data, contentType);

            return(content);
        }
Beispiel #2
0
        /// <summary>
        /// 获得对应的数据处理类
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="streamInvoker"></param>
        /// <param name="result"></param>
        /// <param name="targetStream">目标流</param>
        /// <param name="saveToFilePath">希望保存到的目标文件路径</param>
        /// <param name="extraRequestInfo">额外的请求数据信息</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual HttpResponseContent GetPreferedResponseType <T>(HttpContext ctx, EventHandler <ResponseStreamContent.RequireProcessStreamEventArgs> streamInvoker = null, T result = default(T), Stream targetStream = null, string saveToFilePath = null, ExtraRequestInfo extraRequestInfo = null)
        {
            var resultType = typeof(T);
            var ea         = new GetPreferedResponseTypeEventArgs <T>(this, ctx, result, saveToFilePath, streamInvoker, targetStream, extraRequestInfo);

            ea.ResponseContent = Setting.ContentPayloadBuilder.GetResponseContent(result, ea);

            GlobalEvents.OnBeforeRequestGetPreferedResponseType(this, ea);
            OnBeforeRequestGetPreferedResponseType(this, ea);
            var content = ea.ResponseContent;

            if (content == null)
            {
                if (!saveToFilePath.IsNullOrEmpty())
                {
                    content = new ResponseFileContent(ctx, this, saveToFilePath);
                }
                else if (resultType != typeof(object))
                {
                    if (resultType == typeof(string))
                    {
                        content = new ResponseStringContent(ctx, this);
                    }
                    else if (resultType == typeof(byte[]))
                    {
                        content = new ResponseBinaryContent(ctx, this);
                    }
                    else if (resultType == typeof(Image))
                    {
                        content = new ResponseImageContent(ctx, this);
                    }
                    else if (resultType == typeof(XmlDocument))
                    {
                        content = new ResponseXmlContent(ctx, this, (XmlDocument)(object)result);
                    }
                    else if (resultType == typeof(EventHandler <ResponseStreamContent.RequireProcessStreamEventArgs>))
                    {
                        var r = new ResponseStreamContent(ctx, this);
                        if (streamInvoker != null)
                        {
                            r.RequireProcessStream += streamInvoker;
                        }
                        content = r;
                    }
                    else if (resultType == typeof(Stream))
                    {
                        content = new ResponseCopyStreamContent(ctx, this, targetStream ?? new MemoryStream());
                    }
                    else
                    {
                        content = new ResponseObjectContent <T>(ctx, this)
                        {
                            ObjectInternal = result
                        }
                    };
                }
                else
                {
                    content = null;                     //为null,等待请求自动判断
                }
                ea.ResponseContent = content;
            }

            //Global events
            GlobalEvents.OnRequestGetPreferedResponseType(this, ea);

            //instance events
            OnRequestGetPreferedResponseType(this, ea);

            //http handler
            HttpHandler.GetPreferedResponseType <T>(this, ctx, content, streamInvoker, result, targetStream);

            return(content);
        }