예제 #1
0
 /// <summary>
 /// 验证上传文件的最大字节长度。
 /// </summary>
 /// <param name="name">参数名</param>
 /// <param name="value">参数值</param>
 /// <param name="maxLength">最大长度</param>
 public static void ValidateMaxLength(String name, FileItem value, int maxLength)
 {
     if (value != null && value.GetContent() != null && value.GetContent().Length > maxLength)
     {
         throw new JdException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name));
     }
 }
예제 #2
0
 /// <summary>
 /// 验证上传文件的最大字节长度。
 /// </summary>
 /// <param name="name">参数名</param>
 /// <param name="value">参数值</param>
 /// <param name="maxLength">最大长度</param>
 public static void ValidateMaxLength(String name, FileItem value, int maxLength)
 {
     if (value != null && value.GetContent() != null && value.GetContent().Length > maxLength)
     {
         throw new JdException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name));
     }
 }
예제 #3
0
        /// <summary>
        /// 执行带文件上传的HTTP POST请求。
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="textParams">请求文本参数</param>
        /// <param name="fileParams">请求文件参数</param>
        /// <returns>HTTP响应</returns>
        public String DoPost(String url, IDictionary <String, String> textParams, IDictionary <String, FileItem> fileParams)
        {
            // 如果没有文件参数,则走普通POST请求
            if (fileParams == null || fileParams.Count == 0)
            {
                return(DoPost(url, textParams));
            }

            String boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线

            HttpWebRequest req = GetWebRequest(url, "POST");

            req.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;

            Stream reqStream = req.GetRequestStream();

            Byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            Byte[] endBoundaryBytes  = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

            // 组装文本请求参数
            String textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}";
            IEnumerator <KeyValuePair <String, String> > textEnum = textParams.GetEnumerator();

            while (textEnum.MoveNext())
            {
                String textEntry = String.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
                Byte[] itemBytes = Encoding.UTF8.GetBytes(textEntry);
                reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
                reqStream.Write(itemBytes, 0, itemBytes.Length);
            }

            // 组装文件请求参数
            String fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
            IEnumerator <KeyValuePair <String, FileItem> > fileEnum = fileParams.GetEnumerator();

            while (fileEnum.MoveNext())
            {
                String   key       = fileEnum.Current.Key;
                FileItem fileItem  = fileEnum.Current.Value;
                String   fileEntry = String.Format(fileTemplate, key, fileItem.GetFileName(), fileItem.GetMimeType());
                Byte[]   itemBytes = Encoding.UTF8.GetBytes(fileEntry);
                reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
                reqStream.Write(itemBytes, 0, itemBytes.Length);

                Byte[] fileBytes = fileItem.GetContent();
                reqStream.Write(fileBytes, 0, fileBytes.Length);
            }

            reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            reqStream.Close();

            HttpWebResponse rsp      = (HttpWebResponse)req.GetResponse();
            Encoding        encoding = Encoding.GetEncoding(rsp.CharacterSet);

            return(GetResponseAsString(rsp, encoding));
        }