Very short snippet: NSConnection in with POST parameters

che in modalità POST . The object NSMutableURLRequest can be used in either GET that mode POST . . The parameters, however, are formatted as if they were in GET , ie in the sequence nome_campo1=valore1&nome_campo2=valore2&... . Here's a handy snippet to simplify the construction of golf:

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 * value = [aDictionary objectForKey: key];
( ( i == 0 ) ? @ "%@=%@" : @ "&%@=%@" ) , key, value ] ; [PostString AppendFormat: ((i == 0)? @ "% @ =% @" @ "&% @ =% @"), Key, value];
}

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

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

; [PostString release];
; postString = nil;

; [Request release];
; request = nil;

; return YES;
{ Else {}
; return NO;
}
}

This method makes it possible to send POST data in the dictionary in the past inputs, more readable and easy:

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 request: params];
; [Params release];
; params = nil;

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: CODE ENTRY:
 <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