分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

httpWebRequest

发布时间:2023-09-06 01:33责任编辑:沈小雨关键词:Webhttp

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

原文地址:https://www.cnblogs.com/tianxinrj/p/8150558.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved