Getting error: HttpRequestException: Received an invalid header name: 'Content=Transfer-Encoding'. when using MVC Core to download a file
Getting error: HttpRequestException: Received an invalid header name: 'Content=Transfer-Encoding'. when using MVC Core to download a file
我正在尝试让 MVC 核心 (3.1) 中的控制器下载音频文件以呈现给用户,但我尝试的所有操作都以错误告终:
HttpRequestException: Received an invalid header name: 'Content=Transfer-Encoding'.
我已经尝试过 webclient、httpclient,最近 webrequest/webresponse。我的代码现在看起来像这样(为了安全删除了细节):
WebRequest downloadRequest = WebRequest.Create({url here});
downloadRequest.Headers.Add("Cookie", "{cookie data here};");
downloadRequest.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");
downloadRequest.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
downloadRequest.Headers.Add("Sec-Fetch-Dest", "empty");
downloadRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (WebResponse webResponse = await downloadRequest.GetResponseAsync())
{
return File(webResponse.GetResponseStream(), "audio/wav", wavFile + ".txt");
}
错误在线
using (WebResponse webResponse = await downloadRequest.GetResponseAsync())
使用 fiddler 请求看起来不错,它抱怨的 header 是:Content=Transfer-Encoding: binary,它在 content-disposition 中有文件作为附件。不幸的是,我完全无法控制发送服务器,但这在浏览器中工作正常。谁能告诉我我做错了什么吗?
谢谢
传入的请求看起来格式不正确。 Transfer-Encoding 是它自己的 header 名称。您不能将其分配给另一个 header 的值。据我所知,内容甚至不是标准的 HTTP header 名称。
请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Message_body_information for valid Content-* headers. The Transfer-Encoding header is documented at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding 处的列表。
为框架 4.8 及以下版本编辑
我必须将以下行添加到 app.config 以让客户端处理来自服务器的无效响应。
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
程序本身如下所示。
try
{
var httpclient = new HttpClient();
var response = await httpclient.GetAsync("http://localhost:9091");
var attachment = new byte[1024];
foreach (var h in response.Content.Headers)
{
var e = h.Value.GetEnumerator();
e.MoveNext();
attachment = Encoding.UTF8.GetBytes(e.Current); // Convert UTF-8 encoded string into raw bytes
Console.WriteLine($"{h.Key}, {Encoding.UTF8.GetString(attachment)}"); // Save raw bytes to disk instead of converting back to string and writing to Console
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
为 dotnet 核心编辑
try
{
var s = new Socket(SocketType.Stream, ProtocolType.Tcp);
var a = IPAddress.Parse("127.0.0.1");
await s.ConnectAsync(a, 9091);
var request = "GET http://localhost:9091/";
var outBytes = Encoding.UTF8.GetBytes(request);
var outBuffer = new ArraySegment<byte>(outBytes);
await s.SendAsync(outBuffer, SocketFlags.None);
var inBytes = new byte[1024];
var inBuffer = new ArraySegment<byte>(inBytes);
await s.ReceiveAsync(inBuffer, SocketFlags.None);
var r = Encoding.UTF8.GetString(inBuffer.Array);
}
catch (SocketException exception)
{
Console.WriteLine($"Error: {exception.Message}");
}
我正在尝试让 MVC 核心 (3.1) 中的控制器下载音频文件以呈现给用户,但我尝试的所有操作都以错误告终:
HttpRequestException: Received an invalid header name: 'Content=Transfer-Encoding'.
我已经尝试过 webclient、httpclient,最近 webrequest/webresponse。我的代码现在看起来像这样(为了安全删除了细节):
WebRequest downloadRequest = WebRequest.Create({url here});
downloadRequest.Headers.Add("Cookie", "{cookie data here};");
downloadRequest.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");
downloadRequest.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
downloadRequest.Headers.Add("Sec-Fetch-Dest", "empty");
downloadRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (WebResponse webResponse = await downloadRequest.GetResponseAsync())
{
return File(webResponse.GetResponseStream(), "audio/wav", wavFile + ".txt");
}
错误在线
using (WebResponse webResponse = await downloadRequest.GetResponseAsync())
使用 fiddler 请求看起来不错,它抱怨的 header 是:Content=Transfer-Encoding: binary,它在 content-disposition 中有文件作为附件。不幸的是,我完全无法控制发送服务器,但这在浏览器中工作正常。谁能告诉我我做错了什么吗?
谢谢
传入的请求看起来格式不正确。 Transfer-Encoding 是它自己的 header 名称。您不能将其分配给另一个 header 的值。据我所知,内容甚至不是标准的 HTTP header 名称。
请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Message_body_information for valid Content-* headers. The Transfer-Encoding header is documented at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding 处的列表。
为框架 4.8 及以下版本编辑
我必须将以下行添加到 app.config 以让客户端处理来自服务器的无效响应。
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
程序本身如下所示。
try
{
var httpclient = new HttpClient();
var response = await httpclient.GetAsync("http://localhost:9091");
var attachment = new byte[1024];
foreach (var h in response.Content.Headers)
{
var e = h.Value.GetEnumerator();
e.MoveNext();
attachment = Encoding.UTF8.GetBytes(e.Current); // Convert UTF-8 encoded string into raw bytes
Console.WriteLine($"{h.Key}, {Encoding.UTF8.GetString(attachment)}"); // Save raw bytes to disk instead of converting back to string and writing to Console
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
为 dotnet 核心编辑
try
{
var s = new Socket(SocketType.Stream, ProtocolType.Tcp);
var a = IPAddress.Parse("127.0.0.1");
await s.ConnectAsync(a, 9091);
var request = "GET http://localhost:9091/";
var outBytes = Encoding.UTF8.GetBytes(request);
var outBuffer = new ArraySegment<byte>(outBytes);
await s.SendAsync(outBuffer, SocketFlags.None);
var inBytes = new byte[1024];
var inBuffer = new ArraySegment<byte>(inBytes);
await s.ReceiveAsync(inBuffer, SocketFlags.None);
var r = Encoding.UTF8.GetString(inBuffer.Array);
}
catch (SocketException exception)
{
Console.WriteLine($"Error: {exception.Message}");
}