2011-10-13 14 views

risposta

20

provare questo

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",username.text,password.text]; 
NSLog(@"%@",post); 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@" server link here"]]]; 
[request setHTTPMethod:@"POST"]; 
NSString *json = @"{}"; 
NSMutableData *body = [[NSMutableData alloc] init]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; 
[request setHTTPBody:postData]; 
//get response 
NSHTTPURLResponse* urlResponse = nil; 
NSError *error = [[NSError alloc] init]; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSLog(@"Response Code: %d", [urlResponse statusCode]); 

if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) 
{ 
    NSLog(@"Response: %@", result); 
} 
+1

Probabilmente vorrai cancellare quella '[richiesta setHTTPBody:' seduta lì. –

+0

{[request setHTTPMethod: @ "POST"]; } dà un errore: - non può assegnare un valore a HTTPMethod, è una proprietà get only. Mi potete aiutare? – Aashish

6
// create mutable request 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
// set GET or POST method 
[request setHTTPMethod:@"POST"]; 
// adding keys with values 
NSString *post = @"query=id=123&name=kkk"; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
[request addValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 
+0

Penso che stia chiedendo come inviare gli argomenti ... – lluismontero

+0

aggiornato la mia risposta – Nekto