2010-06-29 4 views
0

웹 사이트에서 정보를 얻고 싶습니다. 이미 웹 사이트의 HTML 코드가 있습니다. 그러나 웹 사이트의 헤더 정보 (예 : http 상태 코드)를 더보고 싶습니다.NSHTTPURL 요청 헤더

사과 라이브러리를 읽은 후 해당 정보를 표시 할 수있는 두 가지 방법이 있음을 발견했습니다. 그러나, 나는 오직 하나의 방법 (경고 있음)을 사용할 수 있으며, 또 다른 방법은 사용할 수 없다.

다음은 코드입니다.

NSHTTPURLResponse *response = nil; 
NSError *error = nil; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

NSString *data=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 

NSDictionary *responseAlllHeaderFields = [response allHeaderFields]; 
// how can I convent the NSDictionary to the NSString, I cannot use the NSLog to display the information 
NSLog(responseAlllHeaderFields); // <-- wrong 

// returns the HTTP status code for the response 
NSInteger *responseStatusCode = [response statusCode]; 
// warning: initialization makes pointer from integer without a cast 
NSLog(@"\nThe status code is %d\n", responseStatusCode); 

게다가 헤더 정보를 얻는 다른 방법이 있습니까?

대단히 감사합니다.

+0

당신은 할 수 있습니다. NSLog (@ "헤더 필드는 % @", responseAllHeaderFields입니다;)' –

답변

1

답장을 보내 주셔서 감사합니다.

그러나 경고가 있습니다.

다음은 위의 3 가지 방법이 잘못 작업이 될 수없는 이유를 나는 아무 생각이없는 코드

NSInteger *responseStatusCode = [[response statusCode] intValue]; 
NSLog(@"\nThe status code is %d\n", responseStatusCode); 

NSInteger *responseStatusCode = [response statusCode]; 
[responseStatusCode intValue]; 
NSLog(@"\nThe status code is %d\n", responseStatusCode); 

NSInteger *responseStatusCode = [response statusCode]; 
NSLog(@"\nThe status code is %d\n", [responseStatusCode intValue]); 

입니다.

감사합니다.

+2

당신은 여전히'responseStatusCode'를'NSInteger * '로 만들고 있습니다. 포인터 캐스트'*'를 버리면 문제가됩니다. –

3

NSInteger가 int의 래퍼이기 때문에 경고 메시지가 나타납니다. 따라서 포인터 *이 필요하지 않습니다. 과제의 왼쪽을 NSInteger responseStatusCode으로 변경하고 경고가 사라지는 지 확인합니다.

관련 문제