tags:

views:

67

answers:

1

Hello all,

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. http://stackoverflow.com/questions/1945479/invoking-a-http-post-url-from-iphone-using-net-web-service/3115149#3115149

Thank you very much.

+1  A: 

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
thank you for you answerbut I found thatwhen I enter the data in the textfield and I press the buttonThe button is in "blue" and there is not changeAnd there are 2 warningWarning 1:passing argument 1 of 'requestWithURL:' from incompatible pointer typeI 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
also, the url is "https", not the "http", I have a crf, but I do not know how to do
Questions
`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
can any 1 help me?
Questions
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
is the system has some problems?I can see you already reply, but I cannot see your post
Questions