Beispiel #1
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);
        }
Beispiel #2
0
        /// <summary>
        /// 创建网络请求
        /// </summary>
        /// <param name="uri">请求地址</param>
        /// <param name="method">方法</param>
        /// <param name="requestData">写入的数据</param>
        /// <param name="refer">引用页</param>
        /// <param name="messageCallback">消息处理句柄</param>
        /// <param name="saveToFile">保存文件地址</param>
        /// <param name="streamInvoker">流读取对象,仅当返回结果为流时可用</param>
        /// <typeparam name="TResult">结果类型</typeparam>
        /// <returns></returns>
        public HttpContext <TResult> Create <TResult>(Uri uri, HttpMethod?method = null, object requestData = null, Uri refer = null,
                                                      Action <HttpRequestMessage> messageCallback = null, string saveToFile = null, Action <Stream> streamInvoker = null, bool async = false)
        {
            var resultType = typeof(TResult);

            if (method == null)
            {
                method = HttpMethod.GET;
                if (requestData != null)
                {
                    method = HttpMethod.POST;
                }
            }
            if (streamInvoker != null && typeof(Stream) == resultType)
            {
                throw new InvalidOperationException("非流结果时不可设置流操作");
            }


            var request = new HttpRequestMessage(uri, method.Value)
            {
                ReferUri = refer,
                Encoding = Setting.StringEncoding
            };

            if (requestData != null)
            {
                if (requestData is HttpRequestContent)
                {
                    request.RequestData = requestData as HttpRequestContent;
                }
                else if (requestData is string)
                {
                    request.RequestData = new RequestStringContent(requestData as string);
                }
                else if (requestData is Stream)
                {
                    request.RequestData = new RequestCopyStreamContent(requestData as Stream);
                }
                else if (requestData is byte[])
                {
                    request.RequestData = new RequestByteBufferContent(requestData as byte[]);
                }
                else if (requestData is IDictionary <string, string> )
                {
                    request.RequestData = new RequestFormDataContent(requestData as IDictionary <string, string>);
                }
                else
                {
                    request.RequestData = new RequestObjectContent <object>(requestData);
                }
            }
            else if (method == HttpMethod.POST)
            {
                request.RequestData = new RequestByteBufferContent(new byte[0]);
            }
            request.Async = async;

            if (messageCallback != null)
            {
                messageCallback(request);
            }

            var ctx = HttpHandler.GetContext <TResult>(this, request);

            //自动设置格式
            if (request.ExceptType == null)
            {
                if (!saveToFile.IsNullOrEmpty())
                {
                    request.ExceptType = new ResponseFileContent(ctx, this, saveToFile);
                }
                else
                {
                    if (resultType != typeof(object))
                    {
                        if (resultType == typeof(string))
                        {
                            request.ExceptType = new ResponseStringContent(ctx, this);
                        }
                        else if (resultType == typeof(byte[]))
                        {
                            request.ExceptType = new ResponseBinaryContent(ctx, this);
                        }
                        else if (resultType == typeof(Image))
                        {
                            request.ExceptType = new ResponseImageContent(ctx, this);
                        }
                        else if (resultType == typeof(XmlDocument))
                        {
                            request.ExceptType = new ResponseXmlContent(ctx, this);
                        }
                        else if (resultType == typeof(Stream))
                        {
                            var r = new ResponseStreamContent(ctx, this);
                            request.ExceptType = r;
                            if (streamInvoker != null)
                            {
                                r.RequireProcessStream += (s, e) => streamInvoker(e.Stream);
                            }
                        }
                        else
                        {
                            request.ExceptType = new ResponseObjectContent <TResult>(ctx, this);
                        }
                    }
                }
            }

            return(ctx);
        }