示例#1
0
        /// <summary>
        /// multipart/form-data类型post数据
        /// </summary>
        public static WebResponse UploadFile(string url, UploadedFile uploadedFile, Dictionary <string, string> dicPostData = null, Action <HttpWebRequest> SetHeaderAction = null)
        {
            HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;

            #region 请求头信息基本设置
            //分界符字符串 拼在请求头中 multipart/form-data; boundary=boundary_string
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            wr.ContentType       = "multipart/form-data; boundary=" + boundary;
            wr.Method            = "POST";
            wr.KeepAlive         = true;
            wr.Credentials       = System.Net.CredentialCache.DefaultCredentials;
            wr.AllowAutoRedirect = false;
            #endregion

            #region 外部设置请求头
            if (SetHeaderAction != null)
            {
                SetHeaderAction(wr);
            }
            #endregion

            Stream rs = wr.GetRequestStream();

            //首先输入分界符字节串
            //提交的数据中分界符要加两个 '-'
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary);
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            #region 输入上传文件的内容
            if (uploadedFile != null && uploadedFile.data.Length > 0)
            {
                var ContentDisposition =
                    Encoding.UTF8.GetBytes(string.Format("\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", uploadedFile.Name, uploadedFile.FileName, uploadedFile.ContentType));

                rs.Write(ContentDisposition, 0, ContentDisposition.Length);
                rs.Write(uploadedFile.data, 0, uploadedFile.data.Length);
            }
            #endregion

            #region 写入表单普通数据内容
            if (dicPostData != null && dicPostData.Count > 0)
            {
                foreach (var kv in dicPostData)
                {
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
                    var ContentDisposition = System.Text.Encoding.UTF8.GetBytes(string.Format("\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n", kv.Key));
                    rs.Write(ContentDisposition, 0, ContentDisposition.Length);

                    var Content = System.Text.Encoding.UTF8.GetBytes(kv.Value);
                    rs.Write(Content, 0, Content.Length);
                }
            }
            #endregion

            #region 输入内容尾,并关闭流
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();
            #endregion

            var webresponse = wr.GetResponse();
            return(webresponse);
        }
示例#2
0
        /// <summary>
        /// multipart/form-data类型post数据
        /// </summary>
        public static WebResponse UploadFile(string url, UploadedFile uploadedFile, Dictionary<string, string> dicPostData = null, Action<HttpWebRequest> SetHeaderAction = null)
        {     
            HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;

            #region 请求头信息基本设置
            //分界符字符串 拼在请求头中 multipart/form-data; boundary=boundary_string
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
            wr.AllowAutoRedirect = false;            
            #endregion

            #region 外部设置请求头
            if (SetHeaderAction != null)
                SetHeaderAction(wr);
            #endregion

            Stream rs = wr.GetRequestStream();

            //首先输入分界符字节串
            //提交的数据中分界符要加两个 '-' 
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary);
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            #region 输入上传文件的内容
            if (uploadedFile != null && uploadedFile.data.Length > 0)
            {
                var ContentDisposition = 
                Encoding.UTF8.GetBytes(string.Format("\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", uploadedFile.Name, uploadedFile.FileName, uploadedFile.ContentType));

                rs.Write(ContentDisposition, 0, ContentDisposition.Length);
                rs.Write(uploadedFile.data, 0, uploadedFile.data.Length);
            }
            #endregion          

            #region 写入表单普通数据内容
            if (dicPostData != null && dicPostData.Count > 0)
            {
                foreach (var kv in dicPostData)
                {
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
                    var ContentDisposition = System.Text.Encoding.UTF8.GetBytes(string.Format("\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n", kv.Key));
                    rs.Write(ContentDisposition, 0, ContentDisposition.Length);

                    var Content = System.Text.Encoding.UTF8.GetBytes(kv.Value);
                    rs.Write(Content, 0, Content.Length);
                }
            }
            #endregion            
            
            #region 输入内容尾,并关闭流
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();
            #endregion

            var webresponse = wr.GetResponse();
            return webresponse;
        }