1

I would like to ask about the button and the url post method in iPhone application.

In my program, I want the user to click a button, and then a url will be called by POST method. For the url, it may need to redirect to somewhere (302 or 303) etc and final is 200.

I have complete the button and the success page, however, I don't know how to use the objective-C library. I found lots of reference of this forum, but I don't understand what the code means. Can anyone help me?

The following is a question which I believe related to the question. Invoking a http post URL from iphone using .net web service

Thank you very much.

Community
  • 1
  • 1
Questions
  • 20,055
  • 29
  • 72
  • 101
  • do you have working code for a 'non-post' button? might be easiest to show the required changes to that code... – drfrogsplat Jun 25 '10 at 07:25

1 Answers1

3

If you're okay with blocking the thread you're making the request on, this is just about as simple as it gets.

NSString *postBody = [NSString stringWithFormat:@"param1=%@&param2", param1value, param2value];
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:targetURL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

A few caveats:

  • This will perform a synchronous (blocking) request, so either do it on a background thread or look into using the NSURLConnection delegate methods.
  • POST data must be URL-encoded, so you may need to do some preprocessing of your parameter values.
  • Redirects will occur automatically until a 200 OK or an error is encountered.
warrenm
  • 31,094
  • 6
  • 92
  • 116
  • thank you for you answer but I found that when I enter the data in the textfield and I press the button The button is in "blue" and there is not change And there are 2 warning Warning 1: passing argument 1 of 'requestWithURL:' from incompatible pointer type I put the URL in the targetURL, "NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:targetURL];" The result is like "NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:"www.exampleURL.com"];" Warning 2: unused variable 'responseData' Is there are any problem my url of the "" symbol? Thank you very much – Questions Jun 25 '10 at 09:21
  • also, the url is "https", not the "http", I have a crf, but I do not know how to do – Questions Jun 25 '10 at 09:43
  • `requestWithURL:` expects an `NSURL*`, not an `NSString *`. Use the class method `URLWithString:` to convert it. And as far as the responseData is concerned, it's up to you whether or not you want to use it. It'll contain the markup of whatever page is served in response to the request. Could you clarify what type of authentication you need to perform against the server? Is the certificate correctly installed on the server? – warrenm Jun 25 '10 at 10:43
  • Thank you for your reply, warrenm, I may miss your comment. I can get the data of the html. And, I am not sure about the server, I only am given the url. Can you mind to provide some library for me to search about the certificate, as I have to ask other what about the certificate and the server. – Questions Jun 29 '10 at 03:37
  • is the system has some problems? I can see you already reply, but I cannot see your post – Questions Jun 29 '10 at 08:13