NSMutableURLRequest Send Variables to Script


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.