- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在 HTTP header 中使用 if-modified-since 来决定是否应该下载文件。应用程序已经过测试,一切正常,但现在当我询问我的 NSHTTPURLResponse 实例 response.statusCode
或 [[response allHeaderFields] objectForKey:@"Last-Modified"]
。
它似乎只是 NSURLResponse。可能的原因是什么?
我读过 this topic但问题对我来说仍然不清楚。提前致谢!更新:一些代码:
NSURL *url = [NSURL URLWithString:urlString];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:url];
[myRequest setHTTPMethod:@"GET"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@"If-Modified-Since"];
myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
NSHTTPURLResponse *response=nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(@"Error sending request: %@", [error localizedDescription]);
}
//As was advised
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
//crash here
NSLog(@"%d", newResp.statusCode);
更新:代码错误 - myRequest 和 request 是不同的变量。问题解决了。
最佳答案
很可能是请求失败,没有生成响应对象。您可以按如下方式检查:
if (response) {
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
NSLog(@"%d", newResp.statusCode);
}
else {
NSLog(@"No response received");
}
正如另一位评论者所建议的,您可能应该包含一个 NSError 对象,以便您可以更有效地检查错误:
NSHTTPURLResponse *response=nil;
NSError *error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error];
然后,您可以在检查响应之前检查错误:
if (error) {
NSLog(@"Error sending request: %@", [error localizedDescription]);
}
关于ios - NSHTTPURLResponse 变成 NSURLResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8892638/
我用这个向 web 服务器发出 http 请求: NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [N
我正在向这个地址发送一个对象:https://sandbox.itunes.apple.com/verifyReceipt 与 NSUrlconnection我正在尝试使用此委托(delegate)方
我有一个简单的 NSURLRequest: [NSURLConnection sendAsynchronousRequest:myRequest queue:queue completionHandl
我在特定文件夹上设置了 NSURLCache(../../Application Support/Offline 所以它不会被操作系统随机删除),然后我发送一个带有 NSURLRequestRetur
我正在尝试在发出后续请求之前处理每个请求的数据。这里有两个方法,第二个方法是在第一个方法中使用 dispatch_semaphore 调用的,以确保请求完成,但是,这不会等到响应实际被接收/处理后才会
下载文件时,我希望能够继续下载。例如: NSFileManager *fm = [NSFileManager defaultManager]; NSString *downloadTempPath =
我在 HTTP header 中使用 if-modified-since 来决定是否应该下载文件。应用程序已经过测试,一切正常,但现在当我询问我的 NSHTTPURLResponse 实例 respo
我有一个验证用户身份的好方法,但是我不知道如何获取请求的状态代码。当我执行 NSLog(@"%@", response); 时,我可以清楚地看到它存在,但我找不到任何方法将其拉出来。有没有一种方法,还
我需要修改 NSURLResponse 中的响应 header 。这可能吗? 最佳答案 我刚刚和一个 friend 讨论过这个问题。我的建议是编写 NSURLResponse 的子类。大致如下: @i
正如 Apple 文档所述,如果协议(protocol)实现未将内容长度报告为响应的一部分,则 NSURLResponse 的 expectedContentLength 可能不会返回该值。那么,有没
我正在尝试找出如何从我的 http 响应中解析 JSON。目前我正在使用 NSURLSession 任务来发送发布请求并获得如下响应和结果: NSURLSessionConfiguration *de
请原谅我问这个愚蠢的问题,但是有人可以告诉我 NSURLResponse 和 NSHTTPURLResponse 之间有什么区别 最佳答案 "NSURLResponse declares the pr
我正在创建一个在每个 View Controller 上调用 Web 服务的应用程序。该应用程序在模拟器上运行良好,但在实际设备上,它运行速度非常慢,或者有时会返回空响应,这就是应用程序因服务器的
如果我的应用程序是从 AppStore 下载的,那么在加载网页时它会经常崩溃,但我无法在 Debug模式下重复崩溃。崩溃日志说: -[NSURLResponse allHeaderFields]: u
我正在向 API 发出 POST 请求,并在 Swift 中成功获得响应。下面是我的代码。 private func getData(url: NSURL) { let config: NSU
这是我的下载代码: let url = NSURL(string:"http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of
我覆盖了 NSURLProtocol 并且需要返回带有特定状态码的 HTTP 响应。 NSHTTPURLResponse 没有 statusCode setter ,所以我尝试用以下方法覆盖它: @i
我正在编写一个使用 NSURLConnection 与服务器连接的应用程序。 在委托(delegate)方法 didreceiveresponse 中,如果状态代码为 404,我将取消连接并显示一条消
我看过很多代码,包括 Apple 的 SimpleURLConnections 示例,它们只是将任何 NSURLResponse 转换为 NSHTTPURLResponse。如果它始终是 NSHTTP
我在这方面相当陌生.. 我有一个运行的 tomcat 服务器,我有一个 web 服务方法,当被调用时,返回一个字符串。当我尝试使用 NSOperationQueue *backgroundQueue
我是一名优秀的程序员,十分优秀!