2011-06-08 16 views
30

के प्रतिक्रिया शीर्षलेख से डेटा पढ़ना सर्वर प्रतिक्रिया में भेजे गए शीर्षलेख से डेटा को मैं कैसे पढ़ सकता हूं। मैं अनुरोध भेजने के लिए NSURLConnection का उपयोग कर रहा हूँ।NSURLConnection

उत्तर

72

URL पर किसी HTTP यूआरएल है, तो NSURLResponse है कि आप अपने कनेक्शन के प्रतिनिधि के -connection:didReceiveResponse: विधि में प्राप्त (या किसी अन्य विधि के माध्यम से) एक NSHTTPURLResponse, जो आप हेडर का उपयोग करने देता है कि एक -allHeaderFields विधि है किया जाएगा।

NSURLResponse* response = // the response, from somewhere 
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields]; 
// now query `headers` for the header you want 
2

मेरे मामले में

NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]); 
    NSDictionary *headers = [response allHeaderFields]; 

अच्छा दृष्टिकोण

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[task response]; 
    if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) { 
     NSDictionary *dictionary = [httpResponse allHeaderFields]; 
     NSLog(@"%@", [dictionary description]); 
    }