注册 登录
编程论坛 C# 论坛

求代码解释

radiofan 发布于 2020-01-06 13:55, 1823 次点击
程序代码:
  string url = "http://" + Request.Url.Authority + "/Open/TESTImgProcess/Add/"+type;

            double timeStamp = ConvertToUnixTimestamp(DateTime.Now);

           // string token = CalculateMD5Hash(appKey + '+' + timeStamp + '+' + appSecret);

            string path = Server.MapPath("~/Files/Current/TEST.jpg");


            string result = "";

            string boundary = "--------------------" + DateTime.Now.Ticks.ToString("x");

            byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

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

            req.Method = "POST";

            Stream rs = req.GetRequestStream();

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("imgid", "test");
           

            string formdataTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}";

            foreach (var item in dic)
            {
                rs.Write(boundaryBytes, 0, boundaryBytes.Length);

                string formItem = string.Format(formdataTemplate, item.Key, item.Value);

                byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(formItem);

                rs.Write(formItemBytes, 0, formItemBytes.Length);
            }

            rs.Write(boundaryBytes, 0, boundaryBytes.Length);

            string headerTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\n";

            string header = string.Format(headerTemplate, "image_file", "test.jpg", "image/jpeg");

            byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(header);

            rs.Write(headerBytes, 0, headerBytes.Length);

            FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read);

            byte[] buffer = new byte[4096];

            int bytesRead = 0;
0 回复
1