How to get HTTP headers

后端 未结 5 1453
有刺的猬
有刺的猬 2020-12-25 11:54

How do you retrieve all HTTP headers from a NSURLRequest in Objective-C?

相关标签:
5条回答
  • 2020-12-25 12:08

    You can use AFHTTPSessionManager framework and get The headers after a request or during, example below (to get all keys):

    NSHTTPURLResponse *reponse = (NSHTTPURLResponse *)task.response; NSLog(@"%@", [reponseHeaders allHeaderFields]);

    To access a specific key: [reponseHeaders allHeaderFields][@"someKey"]

    Refer to: https://github.com/AFNetworking/AFNetworking/issues/3688

    0 讨论(0)
  • 2020-12-25 12:10

    Given NSURLConnection is deprecated from iOS 9, you can use an NSURLSession to get MIME type information from an NSURL or NSURLRequest.

    You ask the session to retrieve the URL, then upon receiving the first NSURLResponse (which contains MIME type information) in a delegate callback you cancel the session to prevent it downloading the whole URL.

    Here is some bare bones Swift code that does it:

    /// Use an NSURLSession to request MIME type and HTTP header details from URL.
    ///
    /// Results extracted in delegate callback function URLSession(session:task:didCompleteWithError:).
    ///
    func requestMIMETypeAndHeaderTypeDetails() {
        let url = NSURL.init(string: "https://google.com/")
        let urlRequest = NSURLRequest.init(URL: url!)
    
        let session = NSURLSession.init(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
    
        let dataTask = session.dataTaskWithRequest(urlRequest)
        dataTask.resume()
    }
    
    //MARK: NSURLSessionDelegate methods
    
    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
    
        // Cancel the rest of the download - we only want the initial response to give us MIME type and header info.
        completionHandler(NSURLSessionResponseDisposition.Cancel)
    }
    
    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?)
    {       
        var mimeType: String? = nil
        var headers: [NSObject : AnyObject]? = nil
    
    
        // Ignore NSURLErrorCancelled errors - these are a result of us cancelling the session in 
        // the delegate method URLSession(session:dataTask:response:completionHandler:).
        if (error == nil || error?.code == NSURLErrorCancelled) {
    
            mimeType = task.response?.MIMEType
    
            if let httpStatusCode = (task.response as? NSHTTPURLResponse)?.statusCode {
                headers = (task.response as? NSHTTPURLResponse)?.allHeaderFields
    
                if httpStatusCode >= 200 && httpStatusCode < 300 {
                    // All good
    
                } else {
                    // You may want to invalidate the mimeType/headers here as an http error
                    // occurred so the mimeType may actually be for a 404 page or
                    // other resource, rather than the URL you originally requested!
                    // mimeType = nil
                    // headers = nil
                }
            }
        }
    
        NSLog("mimeType = \(mimeType)")
        NSLog("headers = \(headers)")
    
        session.invalidateAndCancel()
    }
    

    I've packaged similar functionality in the URLEnquiry project at github which makes it a bit easier to make in-line queries for MIME types and HTTP headers. URLEnquiry.swift is the file of interest that could be dropped into your own project.

    0 讨论(0)
  • 2020-12-25 12:14

    YourViewController.h

    @interface YourViewController : UIViewController <UIWebViewDelegate>
        @property (weak, nonatomic) IBOutlet UIWebView *yourWebView;
    @end
    

    YourViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //Set the UIWebView delegate to your view controller
        self.yourWebView.delegate = self;
    
        //Request your URL
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://website.com/your-page.php"]];
    
        [self.legalWebView loadRequest:request];
    }
    
    //Implement the following method
    - (void)webViewDidFinishLoad:(UIWebView *)webView{
        NSLog(@"%@",[webView.request allHTTPHeaderFields]);
    }
    
    0 讨论(0)
  • 2020-12-25 12:19

    Swift version using Alamofire for efficiency. This is what worked for me:

    Alamofire.request(YOUR_URL).responseJSON {(data) in
    
    if let val = data.response?.allHeaderFields as? [String: Any] {
          print("\(val)")
        }
    }
    
    0 讨论(0)
  • 2020-12-25 12:24

    This falls under the easy, but not obvious class of iPhone programming problems. Worthy of a quick post:

    The headers for an HTTP connection are included in the NSHTTPURLResponse class. If you have an NSHTTPURLResponse variable you can easily get the headers out as a NSDictionary by sending the allHeaderFields message.

    For synchronous requests — not recommended, because they block — it’s easy to populate an NSHTTPURLResponse:

    NSURL *url = [NSURL URLWithString:@"http://www.mobileorchard.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL: url];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: nil];
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
    NSDictionary *dictionary = [response allHeaderFields];
    NSLog([dictionary description]);
    }
    

    With an asynchronous request you have to do a little more work. When the callback connection:didReceiveResponse: is called, it is passed an NSURLResponse as the second parameter. You can cast it to an NSHTTPURLResponse like so:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;    
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *dictionary = [httpResponse allHeaderFields];
        NSLog([dictionary description]);
    }
    }
    
    0 讨论(0)
提交回复
热议问题