using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace Web { public class Post { public static string HttpUploadFile(string id, string token, string filepath) { // 这个可以是改变的,也可以是下面这个固定的字符串 string boundary = "c808bb4d-264c-4484-bb95-11d72a277878"; string url = "https://api.domain.cc/User.updateAvatar"; // 创建request对象 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url); webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; webrequest.UserAgent = "okhttp-okgo/jeasonlzy"; // 构造发送数据 StringBuilder sb = new StringBuilder(); // 文本域的数据 sb.Append("--" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"uid\""); sb.Append("\r\n"); sb.Append("Content-Length: 6"); sb.Append("\r\n\r\n"); sb.Append(id); sb.Append("\r\n"); // 文件域的数据 sb.Append("--" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"token\""); sb.Append("\r\n"); sb.Append("Content-Length: 32"); sb.Append("\r\n\r\n"); sb.Append(token); sb.Append("\r\n"); sb.Append("--" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"20200205162320.png\""); sb.Append("\r\n"); sb.Append("Content-Type: image/png"); //少length ??? sb.Append("\r\n\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); //构造尾部数据 byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream(); // 输入头部数据 requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 输入文件流数据 byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); // 输入尾部数据 requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s); // 返回数据流(源码) return sr.ReadToEnd(); } } }
$filePath='E:\1.png' $url='https://api.domain.cc/updateAvatar' $fileBin=[System.IO.File]::ReadAllBytes($filePath) $en=[System.Text.Encoding]::GetEncoding("iso-8859-1") $fileEnc=$en.GetString($fileBin) $boundary=[System.Guid]::NewGuid().toString() $lf="`r`n" $bodyLines=( "--$boundary", "Content-Disposition: form-data; name=`"uid`"", "Content-Length: 6$lf", "310413", "--$boundary", "Content-Disposition: form-data; name=`"token`"", "Content-Length: 32$lf", "5aea7d9dbd15de0fd8b7e26fd8220e8b", "--$boundary", "Content-Disposition: form-data; name=`"file`"; filename=`"20200205162320.png`"", "Content-Type: image/png$lf", $fileEnc, "--$boundary--$lf" ) -join $lf $r=Invoke-RestMethod -Uri $url -Method Post -ContentType "multipart/form-data; boundary=$boundary" -UserAgent "okhttp-okgo/jeasonlzy" -Body $bodyLines
补充:注意字节的编码方式 不然即使图片上传成功了 也无法正常显示