无法访问图像返回响应

时间:2014-03-26 14:56:56

标签: ios objective-c

我有一个.gif图片,只有在Wifi上才能访问。在某个域上,例如mydomain.mysite.com/myimage.gif,无法访问该图像。

但是在我的生产环境中,图像会变得可访问并返回以下响应:

<NSHTTPURLResponse: 0x156c5200> { URL: } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=86400";
    "Content-Length" = 554;
    "Content-Type" = "image/gif";
    Date = "Wed, 26 Mar 2014 14:47:53 GMT";
    Etag = "\"030329e4e9c91:11c4\"";
    "Last-Modified" = "Tue, 09 Jun 2009 13:17:20 GMT";
    Server = "Microsoft-IIS/6.0";
    "X-Powered-By" = "ASP.NET";
} }

我的目标C代码如下:

NSHTTPURLResponse *response;
    NSError *error = nil;

    NSURL *url = [NSURL URLWithString:currentEnvironment];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response
                                      error:&error];



    if(response != nil) return YES;

    return NO;

解决方案:

我相信这是解决方案:实现了cachePolicy。

NSURL *url = [NSURL URLWithString:currentEnvironment];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response
                                      error:&error];

1 个答案:

答案 0 :(得分:1)

生产服务器正在发送缓存响应的Cache-Control标头。解决方案是指定NSURLRequest不缓存下面实现的内容:

NSURL *url = [NSURL URLWithString:currentEnvironment];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response
                                      error:&error];