Example #1
0
 /// <summary>
 /// 将当前的内容序列化到查询中
 /// </summary>
 public override string SerializeAsQueryString()
 {
     if (PostedFile.Count > 0)
     {
         throw new InvalidOperationException("有文件附件时,不可以作为查询字符串,请使用POST查询。");
     }
     if (ProcessedData.Count == 0)
     {
         return(string.Empty);
     }
     return(ProcessedData.Select(s => s.Key + "=" + s.Value).Join("&"));
 }
        /// <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));
                }
            }
        }
 protected virtual void SerializeDataAsQueryString()
 {
     SerializedDataString = ProcessedData.Select(s => s.Key + "=" + s.Value).Join("&");
 }