Beispiel #1
0
 /// <summary>
 /// 同步的请求方式
 /// </summary>
 /// <param name="request">请求的参数</param>
 /// <param name="completionOption"></param>
 /// <param name="token"></param>
 /// <param name="client"></param>
 /// <returns>自定义的Response结果</returns>
 public static Task <HttpResponseMessage> RestSend(this FyHttpRequest request,
                                                   HttpCompletionOption completionOption,
                                                   CancellationToken token,
                                                   HttpClient client = null)
 {
     return((client ?? GetDefaultClient()).RestSend(request, completionOption, token));
 }
Beispiel #2
0
        /// <summary>
        ///  配置使用的cotent
        /// </summary>
        /// <param name="reqMsg"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        private static void ConfigReqContent(HttpRequestMessage reqMsg, FyHttpRequest req)
        {
            if (req.HttpMethod == HttpMethod.Get)
            {
                req.RequestSet?.Invoke(reqMsg);
                return;
            }

            if (req.HasFile)
            {
                var boundary = GetBoundary();

                var memory = new MemoryStream();
                WriteMultipartFormData(memory, req, boundary);
                memory.Seek(0, SeekOrigin.Begin);//设置指针到起点

                reqMsg.Content = new StreamContent(memory);
                req.RequestSet?.Invoke(reqMsg);

                reqMsg.Content.Headers.Remove("Content-Type");
                reqMsg.Content.Headers.TryAddWithoutValidation("Content-Type", $"multipart/form-data;boundary={boundary}");
            }
            else
            {
                var data = GetNormalFormData(req);

                reqMsg.Content = new StringContent(data);
                req.RequestSet?.Invoke(reqMsg);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 配置请求
        /// </summary>
        /// <returns></returns>
        public static HttpRequestMessage ConfigureReqMsg(FyHttpRequest request)
        {
            var reqMsg = new HttpRequestMessage
            {
                RequestUri = string.IsNullOrEmpty(request.AddressUrl) ? request.Uri : new Uri(request.AddressUrl),
                Method     = request.HttpMethod
            };

            ConfigReqContent(reqMsg, request); //  配置内容
            return(reqMsg);
        }
Beispiel #4
0
        /// <summary>
        ///  执行请求方法
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="completionOption"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task <HttpResponseMessage> RestSend(this HttpClient client, FyHttpRequest request,
                                                          HttpCompletionOption completionOption,
                                                          CancellationToken cancellationToken)
        {
            var reqMsg = ConfigureReqMsg(request);

            if (request.TimeOutMilSeconds > 0)
            {
                client.Timeout = TimeSpan.FromMilliseconds(request.TimeOutMilSeconds);
            }

            return(client.SendAsync(reqMsg, completionOption, cancellationToken));
        }
Beispiel #5
0
 /// <summary>
 /// 写入 Form 的内容值 【 非文件参数 + 文件头 + 文件参数(内部完成) + 请求结束符 】
 /// </summary>
 /// <param name="memory"></param>
 /// <param name="request"></param>
 /// <param name="boundary"></param>
 private static void WriteMultipartFormData(Stream memory, FyHttpRequest request, string boundary)
 {
     foreach (var param in request.FormParameters)
     {
         WriteStringTo(memory, GetMultipartFormData(param, boundary));
     }
     foreach (var file in request.FileParameters)
     {
         //文件头
         WriteStringTo(memory, GetMultipartFileHeader(file, boundary));
         //文件内容
         file.Writer(memory);
         //文件结尾
         WriteStringTo(memory, _lineBreak);
     }
     //写入整个请求的底部信息
     WriteStringTo(memory, GetMultipartFooter(boundary));
 }
Beispiel #6
0
        /// <summary>
        /// 写入请求的内容信息 (非文件上传请求)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private static string GetNormalFormData(FyHttpRequest request)
        {
            var formstring = new StringBuilder();

            foreach (var p in request.FormParameters)
            {
                if (formstring.Length > 1)
                {
                    formstring.Append("&");
                }
                formstring.AppendFormat(p.ToString());
            }
            if (string.IsNullOrEmpty(request.CustomBody))
            {
                return(formstring.ToString());
            }

            if (formstring.Length > 1)
            {
                formstring.Append("&");
            }
            formstring.Append(request.CustomBody);
            return(formstring.ToString());
        }
Beispiel #7
0
 /// <summary>
 /// 同步的请求方式
 /// </summary>
 /// <param name="request">请求的参数</param>
 /// <param name="completionOption"></param>
 /// <param name="client"></param>
 /// <returns>自定义的Response结果</returns>
 public static Task <HttpResponseMessage> RestSend(this FyHttpRequest request,
                                                   HttpCompletionOption completionOption, HttpClient client = null)
 {
     return(RestSend(request, completionOption, CancellationToken.None, client));
 }
Beispiel #8
0
 /// <summary>
 ///  执行请求方法
 /// </summary>
 /// <param name="client"></param>
 /// <param name="request"></param>
 /// <param name="completionOption"></param>
 /// <returns></returns>
 public static Task <HttpResponseMessage> RestSend(this HttpClient client, FyHttpRequest request,
                                                   HttpCompletionOption completionOption)
 {
     return(RestSend(client, request, completionOption, CancellationToken.None));
 }