/// <summary>
        /// 引发 <see cref="RequestWrapRequestContent" /> 事件
        /// </summary>
        /// <param name="sender">引发此事件的源对象</param>
        /// <param name="ea">包含此事件的参数</param>
        public static void OnRequestWrapRequestContent(object sender, RequestWrapRequestContentEventArgs ea)
        {
            var handler = RequestWrapRequestContent;

            if (handler != null)
            {
                handler(sender, ea);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 包装内容
        /// </summary>
        /// <param name="ea"></param>
        /// <returns></returns>
        public HttpRequestContent WrapRequestContent(object data, RequestWrapRequestContentEventArgs ea)
        {
            if (data == null)
            {
                return(null);
            }

            var contextItem = GetBuilderContextItem(data.GetType());

            return(contextItem?.CachedWrapMethod?.Invoke(data, ea));
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// 引发 <see cref="RequestWrapRequestContent" /> 事件
        /// </summary>
        /// <param name="sender">引发此事件的源对象</param>
        /// <param name="ea">包含此事件的参数</param>
        public void OnRequestWrapRequestContent(object sender, RequestWrapRequestContentEventArgs ea)
        {
            var handler = RequestWrapRequestContent;

            handler?.Invoke(sender, ea);
        }