iOS - 获取标题结果 [英] iOS - Get Header Results

查看:77
本文介绍了iOS - 获取标题结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题。我有以下代码获取服务器上文件的mod日期:

   - (void)sendRequestForLastModifiedHeaders 
{
/ *发送文件修改日期请求* /
NSURLRequest * modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:URLInput.text]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[[NSURLConnection alloc] initWithRequest:modReq delegate:self];
}
- (void)连接:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应
{
/ *将响应转换为NSHTTPURLResponse,
调用allHeaderFields属性,然后获取
Last-Modified键。
* /

NSHTTPURLResponse * foo = [(NSHTTPURLResponse *)响应allHeaderFields];

NSString * last_modified = [NSString stringWithFormat:@%@,
[[(NSHTTPURLResponse *)响应allHeaderFields] objectForKey:@Last-Modified]];

NSLog(@Last-Modified:%@,last_modified);

}

我的主要问题如下:



此调用仅通过标头发送吗?如果文件很大,我不希望整个文件被下载。这就是为什么我要检查标题。



+++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++ $ p> NSMutableURLRequest * modReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLInput.text]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[modReq setHTTPMethod:@HEAD];


解决方案

如你所知,你可能正在下载整个文件。关键是用于http请求的http方法。默认情况下,这是一个GET请求。你想要的是一个HEAD请求。你不想要这个文件,你只是希望服务器返回你得到的响应,对吗?



为此,你要使用一个 NSMutableURLRequest setHTTPMethod:用HEAD方法构造一个请求,而不是GET。


A quick question. I have the following code which gets the mod date of a file on a server:

- (void) sendRequestForLastModifiedHeaders
{
    /*  send a request for file modification date  */
    NSURLRequest *modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:URLInput.text]
                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
    [[NSURLConnection alloc] initWithRequest:modReq delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    /*  convert response into an NSHTTPURLResponse, 
    call the allHeaderFields property, then get the
    Last-Modified key.
    */

    NSHTTPURLResponse *foo = [(NSHTTPURLResponse *)response allHeaderFields];

    NSString * last_modified = [NSString stringWithFormat:@"%@",
                            [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Last-Modified"]];

    NSLog(@"Last-Modified: %@", last_modified );

} 

My main question is the following:

Does this call only send over the header? If the file is big I do not want the whole file being downloaded. That is why I'm checking the header.

+++++++++++++++++++++++++++++++++++++++ After the update this works... Thanks now looks like:

NSMutableURLRequest *modReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:               URLInput.text]
                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];   
[modReq setHTTPMethod:@"HEAD"];

解决方案

As you have it now, you're probably downloading the whole file. The key is the http method used for the http request. By default, it's a GET request. What you want is a HEAD request. You don't want the file, you just want the server to return the response that you would get if you did, right?

To do that, you want to use a NSMutableURLRequest and setHTTPMethod: to construct a request with a method of HEAD, instead of GET.

这篇关于iOS - 获取标题结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆