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

查看:1123
本文介绍了使用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天全站免登陆