使用 MultipartFormDataContent 生成的 Content-Type 标头错误 [英] Wrong Content-Type header generated using MultipartFormDataContent

查看:69
本文介绍了使用 MultipartFormDataContent 生成的 Content-Type 标头错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

private static async Task<string> PostTest()
{
    string servResp = "";

    using (var content = new MultipartFormDataContent(boundary))
    {
        content.Add(new StringContent("105212"), "case-id");
        content.Add(new StringContent("1/14/2014"), "dateFrom");
        content.Add(new StringContent("1/15/2014"), "dateTo");

        HttpClientHandler handler = new HttpClientHandler();
        cookieContainer = new CookieContainer();
        handler.CookieContainer = cookieContainer;

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://somewebsite.com/form");
        request.Headers.ExpectContinue = false;
        request.Content = content;

        httpClient = new HttpClient(handler);

        HttpResponseMessage response = await httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();

        servResp = await response.Content.ReadAsStringAsync();
    }

    return servResp;
}

当我运行它时,我在 Fiddler 中看到 Content-Type 标头:

When I run it, I see the Content-Type header in Fiddler:

Content-Type: multipart/form-data; boundary="----CustomBoundary8d0f01e6b3b5daf"

因为边界值在引号中,所以服务器会忽略请求体.如果我删除引号并在 Fiddler Composer 中运行请求,则请求将得到正确处理.

Because the boundary value is in quotes, the server ignores the request body. If I remove the quotes and run the request in Fiddler Composer, the request is being processed correctly.

我尝试添加内容标题:

//request.Content.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
//request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data; boundary=" + boundary);

...但它不起作用,错误消息是:无法添加值,因为标头‘Content-Type’不支持多个值."和值'multipart/form-data,boundary=----CustomBoundary8d0f024297b32d5'的格式无效.",相应地.

... but it didn't work, the error messages were: "Cannot add value because header 'Content-Type' does not support multiple values." and "The format of value 'multipart/form-data, boundary=----CustomBoundary8d0f024297b32d5' is invalid.", correspondingly.

如何将正确的 Content-Type 标头添加到请求中,以便边界值不会被引号括起来?

How can I add the proper Content-Type header to the request so that the boundary value would not be enclosed in quotes?

Content-Type: multipart/form-data; boundary=----CustomBoundary8d0f01e6b3b5daf

推荐答案

通过从 MultipartFormDataContent 中移除标头并在没有验证的情况下重新添加它解决了这个问题:

Solved this by removing the header from MultipartFormDataContent and re-adding it back without validation:

content.Headers.Remove("Content-Type");
content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);

这篇关于使用 MultipartFormDataContent 生成的 Content-Type 标头错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆