gpt4 book ai didi

android - 从 WebApi 提供的下载在 Android 设备上失败,适用于 Asp.Net MVC

转载 作者:行者123 更新时间:2023-11-29 01:22:20 26 4
gpt4 key购买 nike

由于更新版本的 Android WebView(目前在 v48 上运行),我的应用程序的 pdf 下载停止工作。从桌面浏览器下载,WP10 和 iOS 也能正常运行。

行为:
它说,下载已经开始...然后几分钟后我收到一条通知“下载不成功”。

下载来自托管在 Azure 上的 WebApi Controller 。我尝试了大量不同的建议 https header 和 header 组合。我还比较了来自其他网站的 header ,这些网站可以在我的设备上下载。没有成功。
我很好奇,并在 Asp.Net Mvc (5) Controller (同一项目)中下载并修改了 header ,使其与 WebApi (2.2) 对应的 header 完全一样。 => 下载成功!。比较 Fiddler 中的两个响应,除了 Url - 它们看起来完全一样。

知道还有什么不同吗?

这里只是一些代码位:

MVC 测试(工作)

public FileResult CoolDocument()
{
// Some hardcoded binary data (minimum valid pdf)
byte[] bytearr = new byte[] { 37, 80, ... };

// Making headers look the same as in the WebApi response
Response.AddHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
Response.Headers.Add("Expires", "-1");
Response.Headers.Add("Cache-Control", "no-cache");
Response.Headers.Add("X-Application-Version", "1.0.5919.31987");
Response.Headers.Add("X-Environment", "Acceptance");
Response.Headers.Add("Pragma", "no-cache");

return File(bytearr, "application/pdf");
}

WebApi 测试(不工作) 额外的 header 来自过滤器,但我也尝试过没有它们

[HttpGet]
public HttpResponseMessage CoolDocument()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

/// Some hardcoded binary data (minimum valid pdf)
byte[] bytearr = new byte[] { 37, 80, ... };

response.Content = new ByteArrayContent(bytearr);

response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/pdf");

response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment") { FileName = "\"test.pdf\"", };
return response;
}

这是标题在 Fiddler 中的样子:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 2729
Content-Type: application/pdf
Expires: -1
Server: Microsoft-IIS/8.0
X-Environment: Acceptance
X-Application-Version: 1.0.5919.31987
Content-Disposition: attachment; filename="test.pdf"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Mar 2016 16:50:41 GMT

最佳答案

终于知道哪里出了问题。

Android(还有 Windows Phone 8.1)在开始下载时发出两个请求。我假设第一个来自浏览器 (Chrome),第二个来自 android 下载管理器
第二个请求与原始请求有不同的请求 header 。我是在开始远程调试服务器代码后才注意到第二个请求的,因为下载管理器的请求逻辑上没有记录在 chrome 开发者工具中。
无论如何,这第二个请求导致我的“languge-filter”抛出异常,因为通过读取 Accept-Language Header 发生了愚蠢的空指针异常 - 这在第二个请求中不存在。

关于android - 从 WebApi 提供的下载在 Android 设备上失败,适用于 Asp.Net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36054982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com