Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASIHTTPRequest POST iPhone

Here's a portion of the html code I'm trying to submit the textarea, much this textarea this forum uses to type in questions. It's not working, nothing gets sent and the type of response i get back is

NSHTTPURLResponse: 0x617bb20

Though I managed to get it working for the login except i replaced body=%@ with user=%@&pass=%@

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://forums.whirlpool.net.au%@", replyLink]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"application/xml;charset=UTF-8;"];
[request setPostValue:@"test" forKey:@"body"];
[request setDelegate:self];
[request startAsynchronous];

- (void) requestFinished:(ASIHTTPRequest *)request {
//NSString *responseString = [request responseString];
NSLog(@"Response %d : %@", request.responseStatusCode, [request responseString]);

//NSData *responseData = [request responseData];
}

- (void) requestStarted:(ASIHTTPRequest *) request {
NSLog(@"request started...");
}

- (void) requestFailed:(ASIHTTPRequest *) request {
NSError *error = [request error];
NSLog(@"%@", error);
}

Updated code using ASIHTTPRequest.

like image 331
Mark Avatar asked Jun 04 '11 09:06

Mark


1 Answers

ASIHTTPRequest is the way to go here. It's difficult to understand what is exactly wrong with the code you've written (except that it looks like a synchronous request, which is a no-no).

In ASIHTTPRequest you can do this:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:someUrl];
[request setRequestMethod:@"POST"];
[request setPostValue:@"..." forKey:@"user"];
[request setPostValue:@"..." forKey:@"password"];

[request setDelegate:self];
[request startAsyncrhonous];

Then, make sure your class conforms to the ASIHTTPRequestDelegate protocol and implement at the very least, this method:

- (void)requestFinished:(ASIHTTPRequest *)request {
   NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]);
}

You can also handle other methods if you choose, such as:

- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;

You can always download the ASIHTTPRequest project from github at http://github.com/pokeb/asi-http-request.

The docs are located at http://allseeing-i.com/ASIHTTPRequest/ and are fantastic.

Hope this helps!

like image 197
Ben Scheirman Avatar answered Oct 20 '22 00:10

Ben Scheirman