Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a good example of using NSURLRequest and NSHTTPCookies to do a full web application example of logging into a website, storing the SessionID cookie and resubmitting it in future requests.</p> <p><a href="http://idevkit.com/forums/general-sdk/299-nsurlconnection-nshttpcookie.html" rel="noreferrer">NSURLConnection, NSHTTPCookie</a></p> <p><strong>By logix812:</strong></p> <pre><code> NSHTTPURLResponse * response; NSError * error; NSMutableURLRequest * request; request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60] autorelease]; [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]); // If you want to get all of the cookies: NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]]; NSLog(@"How many Cookies: %d", all.count); // Store the cookies: // NSHTTPCookieStorage is a Singleton. [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil]; // Now we can print all of the cookies we have: for (NSHTTPCookie *cookie in all) NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate); // Now lets go back the other way. We want the server to know we have some cookies available: // this availableCookies array is going to be the same as the 'all' array above. We could // have just used the 'all' array, but this shows you how to get the cookies back from the singleton. NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]]; NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies]; // we are just recycling the original request [request setAllHTTPHeaderFields:headers]; request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"]; error = nil; response = nil; NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload