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

时间:2016-12-02 23:34:54

标签: c# forms post multipartform-data

我正在尝试使用.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='^[^\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>

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

我尝试过的一些帖子:

How to submit a multipart/form-data HTTP POST request from C# How to fix 400 Bad Request error? 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

如何制作HTTP POST请求以便成功上传文件?如果您需要进一步说明,请告诉我,并提前感谢您。

1 个答案:

答案 0 :(得分:0)

我能够使用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;
    }