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.
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!
#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
Just like to say I got it working :).
For those who wants hints and are in my position, well look for hidden fields in forms 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 that was a fundamental step in trying to figure the answer.
As an example one of the hidden fields that were in the html code was:
<input type="hidden" name="version" id="version" value="3">
this would translate to
[request setPostValue:#"3" forKey:#"version"];
"forKey"'s are the "name" field in the html and the "setPostValue" is the value in "html"
you can take the above thinking and apply it to simulate a button press
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"];
hopefully that helps others :).
Related
How do you tell when a request is finished (using ASIHTTPRequest)? Here is the code I am using:
[request setDidFinishSelector:#selector(requestFinished:)];
...
-(void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"The request is done.");
}
However, when request is done, "The request is done" is never printed to the log. Am I doing something wrong? Thanks.
maybe your connection is failing? In this case, you should test for - (void)requestFailed:(ASIHTTPRequest *)request
Did you set the request delegate? Example code:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
I forgot to set the delegate to self. Oops!
[request setDelegate:self];
How can i upload a video taken with the camera or contained in the phone memory?
I need to upload it using a PUT method.
Thank you
Code for photo uploading:
- (void)uploadPhoto:(NSData *)data
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"PUT"];
[request addRequestHeader:#"Date" value:timeStamp];
[request addRequestHeader:#"Auth" value:signature];
[request addRequestHeader:#"Client" value:apiClient];
[request addRequestHeader:#"Accept" value:apiAccept];
[request addRequestHeader:#"USER" value:userHeader];
[request appendPostData:data];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"%#",responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
NSLog(#"%#",responseData);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"%#",error);
}
none of the 3 methods at the end are called after i make this request.
Please help me.
Regards,
response string:
|?xml version="1.0" encoding="UTF-8"?|
|error|
|code|InvalidParameter|/code|
|message|Invalid parameter specified (user)|/message|
|/error|
You can do it using ASIHTTPRequest. Here is a sample:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:myVideoData];
[request setRequestMethod:#"PUT"];
NSURL *url = [NSURL URLWithString:#"someurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:year1 forKey:#"year"];
[request setPostValue:appy_level forKey:#"appy_level"];
[request setPostValue:reasons forKey:#"reasons"];
[request setPostValue:country forKey:#"country"];
[request setPostValue:city forKey:#"city"];
[request setPostValue:sex forKey:#"sex"];
[request setRequestMethod:#"POST"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startAsynchronous];
NSLog(#"response -%#",[request responseString]);
[self dismissModalViewControllerAnimated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
NSLog(#"%#",response);
}
any idea why this code return (null) i mean response is null? and this requestFinised: method isnt working even i write[requestsetDidFinishSelector:#selector(requestFinished)];
method too.I am confuesed right now.
The first NSLog won't work because you start your request as asynchronous.
You chould try putting NSLog(#"finished"); in -requestFinished: to check if page is loading or not. It can be an error in your server file (e.g. php fatal error).
Have you tried this:
NSString *response = [[NSString alloc] initWithData:[request data] encoding:NSUTF8StringEncoding];
NSLog(#"%#", response);
I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.
I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me
I am really noob in Xcode and Objective-C
Thanks
Click for the URL-Loading documentation provided by Apple.
Especially Using NSURLConnection looks interesting for you.
Edit:
Another very good and easy-to-use Framework for this task is ASIHTTP:
Click
The easiest way:
- (void)grabURL
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
Asynchronous loading is only slightly more complex:
- (void)grabURLInBackground
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
Does ASIHTTPRequest support HTTPS connections? My connection right now works for a HTTP connection and errors if I try a HTTPS Connection. (Goes into requestFailed and gives me a ASIHTTPErrorRequestDomain)
-(void) getData
{
av.hidden = NO;
[av startAnimating];
NSString *urlString = [IP stringByAppendingString:#"Method1"];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSLog(#"URL = %#",url);
[request setRequestMethod:#"POST"];
[request setPostValue:#"val1" forKey:#"key1"];
[request setPostValue:#"val2" forKey:#"key2"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
//NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
[self parseData:responseData];
[av stopAnimating];
av.hidden = YES;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
[av stopAnimating];
av.hidden = YES;
}
Thanks,
Teja
Whoops, sorry, figured it out -
[request setValidatesSecureCertificate:NO] works for reference.
Thanks to these guys - http://www.iphonedevsdk.com/forum/iphone-sdk-development/29417-asihttprequest-library-works-https.html
EDIT: Since this is getting some upvotes, I'd just like to add that this might not be the best approach for valid SSL certificates. The one I was using was a self-signed certificate, so this was fine.