2009-03-27 14 views
23

Sto provando a inviare una stringa di autenticazione via cookie in NSMutableURLRequest. Sto cercando di creare il NSHTTPCookie attraversoCreare un cookie per NSURLRequest?

+(id)cookieWithProperties:(NSDictionary *)properties 

Ma da nessuna parte sono stato in grado di trovare il modo per specificare le proprietà diverse dalla semplice coppia chiave-valore che ho per l'autenticazione. Quando uso solo la coppia chiave-valore, viene restituito nil.

Qualsiasi esempio, documentazione o riflessione su questo sarebbe molto apprezzato.

+0

Inserisci il codice che stai utilizzando per creare il cookie; hai dato un'occhiata a http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/doc/uid/20001702-425346 – sbooth

risposta

19

Questo è il modo di impostare le proprietà in un cookie:

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
           url, NSHTTPCookieOriginURL, 
           @"testCookies", NSHTTPCookieName, 
           @"1", NSHTTPCookieValue, 
           nil]; 
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties]; 

Nell'esempio di cui sopra: url, testCookies, e 1 sono i valori . Allo stesso modo, NSHTTPCookieOriginURL, NSHTTPCookieName, NSHTTPCookieValue sono i tasti per l'oggetto NSDictionary, come coppie di valori-chiave.

È possibile impostare/ottenere proprietà utilizzando NSDictionary e aggiungere a NSHTTPCookie.

+4

In realtà non ero in grado di farlo funzionare, solo con NSHTTPCookieDomain e NSHTTPCookiePath. Guarda la risposta di jm. Inoltre: http://lists.apple.com/archives/Webkitsdk-dev/2003/Sep/msg00003.html – mfazekas

+0

Sono stato in grado di utilizzare NSHTTPCookieOriginURL fino a quando ho * anche * specificato NSHTTPCookiePath (grazie a jm sotto per il suggerimento) . Si noti che è possibile utilizzare il metodo del percorso sull'URL per fornire il valore (ad esempio [percorso url]). –

37

ho notato, sul mio iPhone 2.2.1, che il cookie non ha ottenuto creato se NSHTTPCookiePath non è specificato, anche se è indicato come "optional" nella documentazione:

Così, lo faccio :

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"domain.com", NSHTTPCookieDomain, 
          @"/", NSHTTPCookiePath, // IMPORTANT! 
          @"testCookies", NSHTTPCookieName, 
          @"1", NSHTTPCookieValue, 
          nil]; 
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties]; 

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil]; 

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; 

[request setAllHTTPHeaderFields:headers]; 
7

Non riuscivo a farlo funzionare.

ho avuto questo lavoro però:

NSMutableURLRequest* ret = [NSMutableURLRequest requestWithURL:myURL]; 
[ret setValue:@"myCookie=foobar" forHTTPHeaderField:@"Cookie"];
6

ho trovato un errore nell'esempio di JM: NSHTTPCookiePath dovrebbe essere @"/", ma non @"\\\\".

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"domain.com", NSHTTPCookieDomain, 
          @"/", NSHTTPCookiePath, // IMPORTANT! 
          @"testCookies", NSHTTPCookieName, 
          @"1", NSHTTPCookieValue, 
          nil]; 
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties]; 

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil]; 

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; 

[request setAllHTTPHeaderFields:headers]; 
+0

Ho aggiornato la mia risposta. Sembra ragionevole, anche se non ho più un iPhone su cui testare. –

0

chiave NSHTTPCookiePath dovrebbe esistere nel dizionario quando si utilizza il metodo

[NSHTTPCookie cookieWithProperties:dictionary] 

se l'utilizzo di NSHTTPCookieDomain o NSHTTPCookieOriginURL. E il valore per NSHTTPCookiePath deve essere @"/" non @"\\".

Problemi correlati