HttpWebRequest文件上传

时间:2019-06-18 09:54:18

标签: c#

我有以下代码,该代码旨在通过Webhook将文件上传到Discord服务器:

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

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("uri");
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";

Stream memStream = new System.IO.MemoryStream();

var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--");

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

string headerTemplate = "Content-Disposition: form-data; name=\"file\"; filename=\"test.exe\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n";

request.Headers.Add(headerTemplate);

但是它引发了异常

  

异常未处理的错误,“指定的值包含无效的CRLF字符。”文件路径位于%userprofile%\ Desktop \ test.exe

0 个答案:

没有答案