[英]Post File and form data to form c#
I am trying to upload a file to a REST endpoint using .NET C#. 我正在尝试使用.NET C#将文件上传到REST端点。 I have tried numerous permutations seen here on SO and other forums with no success.
我尝试了在SO和其他论坛上看到的许多排列,但没有成功。 The closest I get is a 400 response from the server.
我得到的最接近的是来自服务器的400响应。 I could post the code i've tried, but it's well over 200 lines of garbled mess accumulated over about 8 hours, see links below for what i've tried.
我可以发布我尝试过的代码,但是在大约8个小时内累积了200多行乱码,请参见下面的链接以了解我的尝试。
The form below is located at https://domain.com/fileAttachments/formData/ 下面的表单位于https://domain.com/fileAttachments/formData/
Form HTML: 表单HTML:
<form action="https://domain.com/fileAttachments" method="POST" enctype="multipart/form-data">
<!-- File Attachment [0] -->
<p><label>ContentType</label>
<br>
<input name="contentType[0]" value="application/octet-stream" pattern='^[a-zA-Z-]+/[-a-zA-Z0-9.+_*]+$' title='^[a-zA-Z-]+/[-a-zA-Z0-9.+_*]+$' maxlength="128" type="text">
</p>
<p><label>Data</label>
<br>
<input name="data[0]" type="file" required>
</p>
<p><label>FileName</label>
<br>
<input name="fileName[0]" pattern='^[^\t\n\/:*?"<>|]*$' title='^[^\t\n\/:*?"<>|]*$' maxlength="100" type="text" required>
</p>
<p><label>Description</label>
<br>
<textarea name="description[0]" maxlength="1333" rows=6 ></textarea>
</p>
<p><label>Name</label>
<br>
<input name="name[0]" pattern='^[^\t\n]*$' title='^[^\t\n]*$' maxlength="40" type="text">
</p>
<br>
<input type="submit" id="submit" value="Upload">
</form>
The POST headers through a browser POST are: 通过浏览器POST的POST标头为:
Request Payload
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="contentType[0]"
application/octet-stream
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="data[0]"; filename="file.pdf"
Content-Type: application/pdf
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="fileName[0]"
testFileName
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="description[0]"
------WebKitFormBoundary8Z24B55DIvnjkUGF
Content-Disposition: form-data; name="name[0]"
------WebKitFormBoundary8Z24B55DIvnjkUGF--
Some of the posts I have tried: 我尝试过的一些帖子:
How to submit a multipart/form-data HTTP POST request from C# How to fix 400 Bad Request error? 如何从C#提交多部分/表单数据HTTP POST请求 如何解决400 Bad Request错误? Send file+parameters in post request UploadFile with POST values by WebClient How to upload file to server with HTTP POST multipart/form-data c# setting uploadFile content-Type
通过WebClient 在发布请求中 使用POST值 发送文件和参数 UploadFile 如何使用HTTP POST multipart / form-data c#设置uploadFile content-Type 将文件上传到服务器
How do I craft an HTTP POST request so the file is uploaded successfully? 如何制作HTTP POST请求,以便文件成功上传? Please let me know if you need additional clarification, and thank you in advance.
如果您需要其他说明,请告诉我,谢谢。
I was able to use WebRequest.Write to POST the data as a byte-array. 我能够使用WebRequest.Write将数据发布为字节数组。
private string setData(string address, string serializedData, WebMethods type)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
byte[] data = Encoding.ASCII.GetBytes(serializedData);
WebRequest request = WebRequest.Create(address);
CredentialCache mycreds = new CredentialCache();
mycreds.Add(new Uri(_baseAddress), "Basic", new NetworkCredential(_username, _password));
request.Credentials = mycreds;
request.Method = type.ToString();
request.ContentLength = data.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
string responseFromRequest = ((HttpWebResponse)response).StatusDescription;
response.Close();
return responseFromRequest;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.