Sehr kurze Snippet: NSConnection mit POST-Parameter

che in modalità POST . Das Objekt NSMutableURLRequest kann entweder verwendet werden GET diesen Modus POST . . Die Parameter sind jedoch formatiert, als ob sie in waren GET , dh in der Reihenfolge nome_campo1=valore1&nome_campo2=valore2&... . Hier ist eine praktische Schnippsel, um den Bau von Golf vereinfachen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17.
18
19
20
21.
22.
23.
24
25
26.
27.
28.
29.
30
31.
BOOL ) connectionPOST : ( NSURLRequest * ) aRequest - (BOOL) connectionPOST: ( NSURLRequest *) aRequest
NSDictionary * ) aDictionary { withParams: ( NSDictionary *) {aDictionary

aDictionary count ] > 0 ) { if ([aDictionary count]> 0) {
request = [ [ NSMutableURLRequest alloc ] NSMutableURLRequest * request = [[ NSMutableURLRequest alloc]
aRequest URL ] ] ; initWithURL: [aRequest URL]];
@ "POST" ] ; [Request setHTTPMethod: @ "POST"];

postString = [ [ NSMutableString alloc ] init ] ; NSMutableString * postString = [[ NSMutableString alloc] init];
allKeys = [ aDictionary allKeys ] ; NSArray * AllKeys = [aDictionary AllKeys];
int i = 0 ; i < [ allKeys count ] ; i ++ ) { for (int i = 0; i <[AllKeys count]; i + +) {
key = [ allKeys objectAtIndex : i ] ; NSString * key = [AllKeys objectAtIndex: i];
value = [ aDictionary objectForKey : key ] ; NSString *-Wert = [aDictionary objectForKey: key];
( ( i == 0 ) ? @ "%@=%@" : @ "&%@=%@" ) , key, value ] ; [PostString AppendFormat: ((i == 0) @ "% @ =% @" @ "&% @ =% @"), Schlüssel, Wert];
}

[ postString dataUsingEncoding : NSUTF8StringEncoding ] ] ; [Request setHTTPBody: [postString dataUsingEncoding: NSUTF8StringEncoding]];

connectionWithRequest : request delegate : self ] ; [ NSURLConnection connectionWithRequest: Anfrage Delegierter: self];

; [PostString release];
; postString = nil;

; [Request release];
; request = nil;

; zurück JA;
{ Else {}
; zurück NO;
}
}

Diese Methode ermöglicht es, senden POST Daten im Wörterbuch in den letzten Eingänge, besser lesbar und einfach:

1
2
3
4
5
6
7
8
9
10
11
url = [ NSURL URLWithString : @ "http://www.undolog.com" ] ; NSURL * url = [ NSURL URLWithString: @ "http://www.undolog.com"];
request = [ NSURLRequest requestWithURL : url ] ; NSURLRequest * request = [ NSURLRequest requestWithURL: url];

/ / Send name = name = Pluto & Goofy
params = [ [ NSDictionary alloc ] NSDictionary * params = [[ NSDictionary alloc]
initWithObjectsAndKeys:
, @ "nome" , @ "Foo", @ "name",
, @ "cognome" , nil ] ; @ "Pluto", @ "name", nil];
request withParams : params ] ; [Self connectionPOST: withParams Anfrage: params];
; [Params release];
; params = nil;

Es sind keine Kommentare für diesen Beitrag

Hinterlasse einen Kommentar

TAG XHTML ERLAUBT: Code-Eingabe:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL