ASIHTTPRequest POST iPhone

Here's the html code part. I'm trying to send a text field, basically this is the text field that this forum uses to enter questions. It does not work, nothing is sent, and the type of response I receive is

NSHTTPURLResponse: 0x617bb20

Although I managed to get it to work for logging in, except that 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.

+6
source share
3 answers

ASIHTTPRequest is the way here. It's hard to see what exactly is wrong with the code you wrote (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 complies with the ASIHTTPRequestDelegate protocol and implements at least this method:

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

You can also handle other methods if you choose, for example:

 - (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!

+16
source
 #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" #import "JSON.h" #import "JSONKit.h" NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"YOUR URL"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setRequestMethod:@"POST"]; [request setPostValue:Email forKey:@"email"]; [request setPostValue:Password forKey:@"password"]; [request setDelegate:self]; [request setUsername:@"signin"]; [request startAsynchronous]; #pragma mark- Request Finish - (void)requestFailed:(ASIHTTPRequest *)request; { NSLog(@"********** RequestFailed **************%@",request.responseString); [SVProgressHUD dismiss]; } -(void)requestFinished:(ASIHTTPRequest *)request { SBJSON *parser = [[SBJSON alloc] init]; NSDictionary *resDict = [parser objectWithString:[request responseString] error:nil]; if ([[request username] isEqualToString:@"signin"]) { } [SVProgressHUD dismiss]; } 

AFNetworking Files: https://drive.google.com/file/d/0B_RDiggCq5U3enh2WUdKaVlRcGM/view?usp=sharing

ASIHTTPREQUEST Files: https://drive.google.com/file/d/0B_RDiggCq5U3LUttRm9WSS1KN2s/view?usp=sharing

+2
source

Just how to say, I got his job :).

For those who want hints and are in my place, look for hidden fields in forms well or check this site http://www.echoecho.com/htmlforms07.htm

and this post to see what i mean

http://www.iphonedevsdk.com/forum/148450-post14.html

For those who just want a more direct answer, keep reading, but please read the iphonedevsdk link, which was a fundamental step in trying to understand the answer.

As an example, one of the hidden fields that were in the html code was the following:

 <input type="hidden" name="version" id="version" value="3"> 

it will translate to

 [request setPostValue:@"3" forKey:@"version"]; 

"forKey" is the "name" field in html, and "setPostValue" is the value in "html"

you can take the above thinking and apply it to simulate a button click from

 <input type="submit" name="post" id="post" tabindex="56" style="width:150px;font:16px Arial;" value="Post Reply" onc... 

to

 [request setPostValue:@"submit" forKey:@"post"]; 

hope this helps others :).

+1
source

Source: https://habr.com/ru/post/889762/


All Articles