Posted By

tbass134 on 01/20/10


Tagged

phpobjective CNSMutableURLRequest


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

mracoker
avanadra
bobbym245
slav123
iure
Tintenklecks


NSMutableURLRequest Send Variables to Script


 / Published in: Objective C
 

  1. NSURL *url = [NSURL URLWithString:@"http://server.com/file.php"];
  2.  
  3. NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url
  4. cachePolicy:NSURLRequestReloadIgnoringCacheData
  5. timeoutInterval:60];
  6.  
  7. [req setHTTPMethod:@"POST"];
  8. [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  9.  
  10. NSString *postData = [NSString stringWithFormat:@"page=%@&param1=%@&param2=%@param3=%@", @"item1", @"item 2",@"item 3",@"item 4"];
  11.  
  12. NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
  13. [req setValue:length forHTTPHeaderField:@"Content-Length"];
  14.  
  15. [req setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
  16.  
  17. NSHTTPURLResponse* urlResponse = nil;
  18. NSError *error = [[[NSError alloc] init] autorelease];
  19.  
  20. NSData *responseData = [NSURLConnection sendSynchronousRequest:req
  21. returningResponse:&urlResponse
  22. error:&error];
  23.  
  24. NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

Report this snippet  

You need to login to post a comment.