2011-08-31 5 views
11

빠른 질문.iOS - 헤더 결과 얻기

이 호출은 헤더를 통해 전송 하는가 :

- (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); 

} 

내 주요 질문은 다음과 같다 : I는 서버에있는 파일의 모드 날짜를 얻을 다음 코드가? 파일이 크면 전체 파일을 다운로드하지 않기를 바랍니다. 그래서 헤더를 확인하고 있습니다. 이 작품 업데이트 후

+++++++++++++++++++++++++++++++++++++++ . .. 같은 덕분에 지금 보이는 : 당신이 지금 그것을 가지고, 당신은 아마 전체 파일을 다운로드하는

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

파일의 새 복사본을 다운로드해야하는지 여부를 확인하기 위해 마지막으로 수정 한 헤더가 있는지 확인하고 있습니까? 그렇다면 [ASIHTTPRequest'] (http://allseeing-i.com/ASIHTTPRequest)와 같이 미리 만들어진 HTTP 요청 라이브러리를 사용하는 것이 좋습니다. 귀하가 설정할 수있는 다양한 캐시 정책은 귀하의 상황에 적합해야합니다. – darvids0n

+0

HTTP가 어떻게 작동하는지 알고 싶다면 [RFC 2616] (http://tools.ietf.org/html/rfc2616)을 살펴보십시오. 섹션 9에서는 GET, HEAD, POST 등 다양한 메소드를 정의합니다.이 세 메소드 이외의 메소드는 거의 사용되지 않습니다. –

+0

@iOSGuy 당신이 트위터에 있습니까? – MarkP

답변

27

. 키는 http 요청에 사용되는 http 메소드입니다. 기본적으로 GET 요청입니다. 원하는 것은 HEAD 요청입니다. 파일을 원하지 않는다면, 서버가 원한다면 얻을 수있는 응답을 반환하기를 원할 것입니다. 그렇습니까?

이렇게하려면 NSMutableURLRequestsetHTTPMethod:을 사용하여 GET 대신 HEAD 메서드를 사용하여 요청을 구성해야합니다.

+0

감사합니다 ... 인생을 훨씬 쉽게 ... – iOSGuy

+0

@iOSGuy : 옆에있는 체크 표시를 눌러이 대답을 수락하십시오. – darvids0n