关于http post上传文件
呼叫版主,呼叫各位大侠!不要myfll,用不了。可能和服务端的解释机制有关。
请求url:http://xxx.xxx.x.xx/open/testprocess/add/{imagid},请求中包含文件
返回参数:……
多谢!
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;
[此贴子已经被作者于2020-1-2 08:02编辑过]