httpWebRequest

Posted tianxinrj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpWebRequest相关的知识,希望对你有一定的参考价值。

public static string HttpUpload(string url, List<Dictionary<string, string>> picpathlist, Dictionary<string, string> a)
        {
            string rootUrl = ConfigHelper.AppSettings("RemoteHttpURL");
            // 设置参数
            //初始化HttpWebRequest
            HttpWebRequest request = WebRequest.Create(rootUrl + url) as HttpWebRequest;
            //封装cookie
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;//于此请求关联的cookie
            request.AllowAutoRedirect = true;//请求是否应跟随重定向响应
            request.Method = "POST";//请求方式为post
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线(边界字符串,用来区分各个数据)
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary + "\r\n";//声明数据类型为multipart/form-data(大一发送二进制流的文件数据)
            //byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");//post中的换行符\r\n。(每一个边界符前面需要加两个--,然后跟上换行符)
            byte[] middleBoundaryBytes = Encoding.UTF8.GetBytes("\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
            Stream postStream = request.GetRequestStream();
            foreach (Dictionary<string, string> dic in picpathlist)
            {
                string path = dic["path"];
                //postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
                int pos = path.LastIndexOf("\\");
                string fileName = path.Substring(pos + 1);

                //请求头部信息
                StringBuilder sbHeader = new StringBuilder(string.Format("--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n", dic["type"], dic["parentid"]));
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] bArr = new byte[fs.Length];
                fs.Read(bArr, 0, bArr.Length);
                fs.Close();
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(bArr, 0, bArr.Length);
                postStream.Write(middleBoundaryBytes, 0, middleBoundaryBytes.Length);
            }
            foreach (KeyValuePair<string, string> kv in a)
            {
                //请求头部信息
                StringBuilder sbHeader = new StringBuilder(string.Format("--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\"\r\n\r\n\"{1}\"", kv.Key, kv.Value));
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(middleBoundaryBytes, 0, middleBoundaryBytes.Length);

            }
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }

























































以上是关于httpWebRequest的主要内容,如果未能解决你的问题,请参考以下文章