我有一个UIWebView
从需要身份验证的 Web 服务器下载文件(PDF、Word、Excel)。为此,我已经NSMutableData
按照 Apple 的建议处理了身份验证并填充了一个对象。
将数据加载到UIWebView
我不想对 Mime 类型进行硬编码时,关于如何避免这种情况的任何想法?
[_webView loadData:data MimeType: @"application/msword" textEncodingName: @"UTF-8" baseURL:[NSURL URLWithString: @""]];
尝试了以下但总是返回“text/html”:
- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSString *mimeType = [httpResponse MIMEType];
NSLog(@"MIMETYPE: %@",mimeType);
}
响应标头在这里:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 50176
Content-Type: application/msword
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename=test.doc
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
编辑: 标题发生了一些奇怪的事情。
我最初发布的标头已在 Chrome 和 Fiddler2 中验证,但 NSURLConnection 实际上正在接收:
"Content-Type" = "text/html;charset=us-ascii,application/msword";
"内容长度" = "341, 79360";
以下代码段用于确认这一点:
- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
}
}
知道为什么它会收到这个无效的标题,我很难过?