c# – 使用MultipartFormDataContent生成错误的Content-Type头文件

c# – 使用MultipartFormDataContent生成错误的Content-Type头文件,第1张

概述我有以下代码: private static string boundary = "----CustomBoundary" + DateTime.Now.Ticks.ToString("x");private static async Task<string> PostTest(){ string servResp = ""; using (var content = ne 我有以下代码:
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标题:

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

因为边界值是引号,服务器将忽略请求体.如果我删除引号并在fiddler Composer中运行请求,则请求被正确处理.

我尝试添加内容标题:

//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“无效”,相应地.

如何在请求中添加适当的Content-Type标题,以便边界值不会用引号括起来?

Content-Type: multipart/form-data; boundary=----CustomBoundary8d0f01e6b3b5daf
解决方法 通过从MultipartFormDataContent中删除标题并重新添加它而不进行验证来解决此问题:
content.headers.Remove("Content-Type");content.headers.TryAdDWithoutValIDation("Content-Type","multipart/form-data; boundary=" + boundary);
总结

以上是内存溢出为你收集整理的c# – 使用MultipartFormDataContent生成错误的Content-Type头文件全部内容,希望文章能够帮你解决c# – 使用MultipartFormDataContent生成错误的Content-Type头文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1237093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存