コード例 #1
0
        /// <summary>
        /// 计算长度
        /// </summary>
        /// <returns></returns>
        public override long ComputeLength()
        {
            if (ContentType == ContentType.FormData)
            {
                if (string.IsNullOrEmpty(RequestBoundary))
                {
                    RequestBoundary = "ifish_network_client_" + Guid.NewGuid().ToString().Replace("-", "");
                }

                var boundary = RequestBoundary;
                WebRequset.ContentType = "multipart/form-data; boundary=" + boundary;

                var size = ProcessedData.Where(s => s.Value != null).Select(
                    s =>
                    (long)2                                                                                                                         //--
                    + boundary.Length                                                                                                               //boundary
                    + 2                                                                                                                             //\r\n
                    + 43                                                                                                                            // content-disposition
                    + s.Key.Length                                                                                                                  //key
                    + Message.Encoding.GetByteCount(s.Value)                                                                                        //value
                    + 2                                                                                                                             //\r\n
                    ).Sum();

                //attach file
                size += PostedFile.Sum(s =>
                {
                    s.AttachContext(Context);
                    return(s.ComputeLength());
                });
                size += 4 + boundary.Length;                 //结束标记
                return(size);
            }
            else
            {
                if (ContentType == ContentType.FormUrlEncoded)
                {
                    return(SerializedDataString.Length);
                }
                else
                {
                    return(Math.Max(0, ProcessedData.Select(s => s.Key.Length + 1 + (s.Value == null ? 0 : (ContentType == ContentType.FormUrlEncoded ? s.Value.Length : Message.Encoding.GetByteCount(s.Value)))).Sum() + ProcessedData.Count - 1));
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 异步将数据写入当前的请求流中
        /// </summary>
        /// <param name="asyncData"></param>
        public override void WriteToAsync(AsyncStreamProcessData asyncData)
        {
            base.WriteToAsync(asyncData);

            //异步写入的时候,如果没有文件,则一次性写入
            if (ContentType == ContentType.FormUrlEncoded)
            {
                asyncData.AsyncStreamWrite((ContentType == ContentType.FormUrlEncoded ? System.Text.Encoding.ASCII : Message.Encoding).GetBytes(ProcessedData.Where(s => s.Value != null).Select(s => s.Key + "=" + s.Value).Join("&")), false, null);
                return;
            }

            //否则先写入普通数据,再写入文件。
            byte[] textBuffer = null;
            using (var ms = new MemoryStream())
            {
                //写入普通区域
                foreach (var v in ProcessedData)
                {
                    if (v.Value == null)
                    {
                        continue;
                    }

                    var str = "--" + RequestBoundary + "\r\nContent-Disposition: form-data; name=\"" + v.Key + "\"\r\n\r\n";
                    ms.Write(Message.Encoding.GetBytes(str));
                    if (!v.Value.IsNullOrEmpty())
                    {
                        ms.Write(Message.Encoding.GetBytes(v.Value));
                    }
                    ms.Write(Message.Encoding.GetBytes("\r\n"));
                }
                ms.Close();
                textBuffer = ms.ToArray();
            }

            asyncData.AsyncStreamWrite(textBuffer, true, _ =>
            {
                _currentAsyncFileIndex = 0;

                if (AsyncData.Exception != null)
                {
                    return;
                }

                WriteFileAsync();
            });
        }
コード例 #3
0
        /// <summary>
        /// 写入内容到流中
        /// </summary>
        /// <param name="stream"></param>
        public override void WriteTo(Stream stream)
        {
            if (PostedFile.Count > 0 || ContentType == ContentType.FormData)
            {
                //写入普通区域
                foreach (var v in ProcessedData)
                {
                    if (v.Value == null)
                    {
                        continue;
                    }

                    var str = "--" + RequestBoundary + "\r\nContent-Disposition: form-data; name=\"" + v.Key + "\"\r\n\r\n";
                    stream.Write(Context.Request.Encoding.GetBytes(str));
                    if (!v.Value.IsNullOrEmpty())
                    {
                        stream.Write(Context.Request.Encoding.GetBytes(v.Value));
                    }
                    stream.Write(Context.Request.Encoding.GetBytes("\r\n"));
                }

                //写入文件
                PostedFile.ForEach(s => s.WriteTo(stream));
                var endingstr = "--" + RequestBoundary + "--";
                stream.Write(Context.Request.Encoding.GetBytes(endingstr));
            }
            else
            {
                stream.Write((ContentType == ContentType.FormUrlEncoded ? System.Text.Encoding.ASCII : Message.Encoding).GetBytes(ProcessedData.Where(s => s.Value != null).Select(s => s.Key + "=" + s.Value).Join("&")));
            }
        }