winform模拟asp.net的表单上传文件

public static string DoPost(string url, IDictionary<string, string> textParams, IDictionary<string, FileItem> fileParams, int second)
        {
            try
            {
                // 如果没有文件参数,则走普通POST请求
                if (fileParams == null || fileParams.Count == 0)
                {
                    return DoPost(url, textParams);
                }

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

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.KeepAlive = true;


                //req.UserAgent = "Top4Net";
                //if (System.Web.HttpContext.Current != null && HttpContext.Current.Request != null)
                //    req.UserAgent = HttpContext.Current.Request.UserAgent;

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

                if (second > 0) //超时时间
                    req.Timeout = 1000 * second;

                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);
            }
            catch (Exception ex) { Logs.AppError(url + Environment.NewLine + ex.ToString()); return ex.Message; }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值