发布文件和表单数据以形成c#

我正尝试使用.NET C#将文件上载到REST端点。 我尝试过在SO和其他论坛上看到无数排列,但都没有成功。 我得到的最接近的是来自服务器的400响应。 我可以发布我已经尝试过的代码,但它已经累积了大约8个小时的200多行乱码,请参阅下面的链接以了解我所尝试的内容。

下面的表格位于https://domain.com/fileAttachments/formData/

表单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='^[^tn/:*?"<>|]*$' title='^[^tn/:*?"<>|]*$'  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='^[^tn]*$' title='^[^tn]*$'  maxlength="40"  type="text">
</p>
<br>
<input type="submit" id="submit" value="Upload">
</form>

通过浏览器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--

我尝试过的一些帖子:

如何从C#提交multipart / form-data HTTP POST请求如何修复400错误请求错误? 在POST请求中发送文件+参数UploadFile和WebClient的POST值如何使用HTTP POST multipart / form-data将文件上载到服务器c#setting uploadFile content-Type

如何制作HTTP POST请求以便文件成功上传? 如果您需要进一步澄清,请让我知道,并提前致谢。


我能够使用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;
    }
链接地址: http://www.djcxy.com/p/75403.html

上一篇: Post File and form data to form c#

下一篇: android multipart image upload with json object