c# - HttpRequest.Content.IsMimeMultipartContent() 在应该返回 true 时返回 false

标签 c# rest http httpwebrequest

我需要将 HTTP 请求作为 MultiPartFormData 发送到 REST Controller 。它正在工作,但现在我对我的 Controller 进行的检查声称请求的类型不正确,即使我可以在调试器中看到请求的类型正确。供引用:

enter image description here

这是调用它的控制台应用程序代码:

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace QuickUploadTestHarness
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            using (var content = new MultipartFormDataContent())
            {
                // Make sure to change API address
                client.BaseAddress = new Uri("http://localhost");

                // Add first file content 
                var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
                fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "testData.txt"
                };

                //Add Second file content
                var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
                fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "Sample.txt"
                };

                content.Add(fileContent1);
                content.Add(fileContent2);

                // Make a call to Web API
                var result = client.PostAsync("/secret/endpoint/relevant/bits/here/", content).Result;

                Console.WriteLine(result.StatusCode);
                Console.ReadLine();
            }
        }
    }
}

它怎么可能被解释为不是 MultiPartFormData?请注意请求的“使用 MultiPartFormDataContent

最佳答案

对于 MultiPartFormDataContent,您可以尝试使用 content.Add 重载,它接受一个 namefilename 参数. MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)

关于c# - HttpRequest.Content.IsMimeMultipartContent() 在应该返回 true 时返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772792/

相关文章:

c# - C#使线程等待计时器

java - Rest Web 服务精确返回 double

php - 创建自定义状态代码

javascript - 检查删除和更新 ui angular ngResource 模块

http - 发送 404 或 410 是否符合犹太洁食标准,但仍显示内容?

android - 为 Android 应用程序和 Django 数据库制作 RESTful http 服务?

c# - 路易斯异常 : Operation returned an invalid status code 'Forbidden'

c# - 具有多个客户端和 Web 界面的复杂套接字应用程序

c# - WPF 中的预测键入功能

java - Spring 启动: @GetMapping with Pageable as request parameter don't work as expected