private string UploadRequest(string url, string path, string name)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.MaximumResponseHeadersLength = 1024;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=----WebKitFormBoundary" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n------WebKitFormBoundary" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n------WebKitFormBoundary" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
//请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
StringBuilder sbHeader2 = new StringBuilder("Content-Disposition:form-data;name=\"name\"\r\n\r\n");
byte[] namebyte = Encoding.UTF8.GetBytes(name);
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
byte[] postHeaderBytes2 = Encoding.UTF8.GetBytes(sbHeader2.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes2, 0, postHeaderBytes2.Length);
postStream.Write(namebyte, 0, namebyte.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
string header = "\r\n----WebKitFormBoundary" + boundary + "\r\n" + sbHeader.ToString() + " xx " + "\r\n----WebKitFormBoundary" + boundary + "\r\n" + sbHeader2.ToString() + "test234" + "\r\n----WebKitFormBoundary" + boundary + "--\r\n";
postStream.Close();
HttpWebResponse res;
try
{
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd();
return content;
}
C#客户端传文件 对应java的 MultipartFile
最新推荐文章于 2025-09-29 12:47:43 发布
这段代码展示了如何使用C#的HttpWebRequest类来构造一个POST请求,以上传文件到Java服务器端的MultipartFile接口。通过设置请求头、内容类型和边界,以及读取文件内容并写入请求流,实现了文件的上传操作。
5376

被折叠的 条评论
为什么被折叠?



