ASI HTTP Request for posting a review - iphone

In my app I am sending a ASI HTTP post request so I user can post a review to my web service however I'm having some trouble sending the request it always seems to be failing I'm sure its a simple error I'm over looking but its driving me made.
My Create Review Class is as follows:
-(void)Submit
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://furious-ice-356.heroku.com/places/%#/reviews.xml",self.identifier]];
//[NSURL URLWithString:[NSString stringWithFormat:#"https://furious-ice-356.heroku.com///places/%#/reviews.xml",self.identifier]];
self.bestnight.text = #"Monday";
ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url];
[request1 setUsername:self.username];
[request1 setPassword:self.password];
[request1 setRequestMethod:#"POST"];
[request1 setPostValue:self.bestnight.text forKey:#"review[best-night]"];
[request1 setPostValue:self.comments.text forKey:#"review[comments]"];
[request1 setPostValue:self.rating.text forKey:#"review[rating]"];
[request1 setDelegate:self];
[request1 startAsynchronous];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(#"FINISH Response received: %#", [request responseString]);
UIAlertView * av = [[[UIAlertView alloc] initWithTitle:#"Thank You" message:#"Your review was successfully posted. Thank You for making our App Better." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] autorelease];
[av show];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(#"FAILED Response received: %#", [request responseString]);
}

Try adding:
[request setShouldPresentCredentialsBeforeChallenge:NO];
Or if that doesn't work, try calling the same but passing 'YES'. Also as others have said double check your username/password.

Related

Uploading video on YouTube

I need to upload video from my iphone app to youtube, i have got the access token form google oauth but getting problem while uploading video to youtube, for post request to upload video i am using ASIFormDataRequest. Below is my code, please help me to resolve my mistake, thanks in advance.
- (IBAction)buttonTouched:(id)sender
{
NSLog(#"%d %#",[sender tag],[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"]);
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30, 90, 200, 20.0)];
[alertView addSubview:progressView];
[alertView show];
[progressView setProgress:0];
NSString *name=[videosToShow objectAtIndex:[sender tag]];
NSString *newFilePath = [NSString stringWithFormat:#"%#.mov", [self.path stringByAppendingPathComponent:name]];
NSLog(#"Content URL :%#;", newFilePath);
NSURL *url = [NSURL URLWithString:#"https://uploads.gdata.youtube.com/feeds/api/users/default/uploads"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setRequestMethod:#"POST"];
NSLog(#"Auth Token== %#",[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"]);
[request setPostValue:[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"] forKey:#"authentication_token"];
[request setPostValue:devKey forKey:#"developer_key"];
[request setPostValue:#"abcabc" forKey:#"boundary_string"];
[request setPostValue:devKey forKey:#"content_length"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"xmlcontent" ofType:#"txt"];
NSString *strXml = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[request setPostValue:strXml forKey:#"API_XML_Request"];
[request setPostValue:#"video/mov" forKey:#"video_content_type"];
[request setPostValue:#"video/mov" forKey:#"Binary File Data"];
[request setFile:newFilePath withFileName:#"1.mov" andContentType:#"video/mov" forKey:#"uploaded"];
[request setUploadProgressDelegate:self];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request setShowAccurateProgress:YES];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congrats!!!" message:#"File has been downloaded."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)requestFailed:(ASIFormDataRequest *)request1 {
NSError *error = [request1 error];
NSLog(#"%#", error);
}
Below is the file xmlcontent.txt
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Bad Wedding Toast</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>toast, wedding</media:keywords>
</media:group>
</entry>
Below is the error that i am getting
Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed"
UserInfo=0x1c8ed0 {NSLocalizedDescription=Authentication needed}
You are using the GData library but haven't linked/added it. Check out GData Objective-C Client Setup in XCode 4 on how to build/link the GData library correctly.

Login to system in iphone.

I am new in iphone internet programming. I am writing an app which has login screen.
I am using ASIHTTPREQUEST, this is the link myurl.com/index.php what I want to login. In chrome, I looked at from the developer console this url posts to myurl.com/ajax/login.php when I login the system with username and password.
Anyway, when I click the login button after entering username and password in app, it doesn't login the system.It prints the FAIL:1 ( when I write myurl.com/ajax/login.php, FAIL:1 seen in the browser.)
Detailed description of my problem:
"There is an login screen in myurl.com/index.php. And in web browser, if I write my username and passw, It posts to myurl.com/ajax/login.php ( from the developer console). Anyway, in app I have written myurl.com/index.php as a url but when I request it prints the html elements of that page in console. If I write myurl.com/ajax/login.php as a url, it prints FAIL:1 in console. ( normally in web browser I have written myurl.com/ajax/login.php, it gives an FAIL:1)"
Edit: I have solved problem, "forKey: #"username" need to be equal "forKey: #"ID" and "forKey: #"password" need to be equal "forKey: #"PWD". Because in developer console username and password returned as a ID and PWD. Thanks for comment and answer.
This is the code:
-(IBAction)login:(id)sender
{
NSURL *url = [[NSURL alloc] initWithString:#"https://myurl.com/index.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setTimeOutSeconds:120];
[request setPostValue:studentid.text forKey:#"username"];
[request setPostValue:starspassword.text forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
[url release];
}
-(void) requestFinished:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"Success" message:#"http works"
delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil] autorelease];
[alert show];
NSLog(#"SUCCESS: %#", responseString);
//Request succeeded
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}
-(void) requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"ERROR: %#", [error localizedDescription]);
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"fail" message:#"http fails" delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil]autorelease];
[alert show];
//Request failed
}
Sorry for my english, there are some examples about this topic but I didn't solve this problem. Thanks for your advice.
Have you tried this instead? ASI includes a way to do basic authentication
[request setUsername:studentid.text];
[request setPassword:starspassword.text];
Scroll down to Handling HTTP Authentication
http://allseeing-i.com/ASIHTTPRequest/How-to-use

ASIHTTPRequest login page

I try to make an iphone application that can login to the web application that use the https securing the user information.Now i am stuck in the login page. I don't know how to check the real account in the website of user when logging in by my application. I got the response only 200 even if i put the wrong account.
here is my code:
- (IBAction)clickOK:(id)sender {
NSURL *url = [NSURL URLWithString:#"https://www.freelancer.com/users/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request setRequestMethod:#"POST"];
[request setPostValue:usernameField.text forKey:#"username"];
[request setPostValue:passwordField.text forKey:#"passwd"];
request.timeOutSeconds = 30;
[request setDidFailSelector:#selector(requestLoginFailed:)];
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request startAsynchronous];
}
- (void)requestLoginFailed:(ASIHTTPRequest *)request
{
//notify user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error sending request to the server" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
int statusCode = [request responseStatusCode];
NSString *statusMessage = [request responseStatusMessage];
NSLog(#"StatusCode: %d", statusCode);
NSLog(#"StatusMessage: %#", statusMessage);
}
Can anyone suggest me how to check the real account of this website and keep user login?
Thanks for any help
You've started something Asynchronously
[request startAsynchronous];
And then just expected it to return something straight away:
NSLog(#"%#",[request responseString]);
NSLog(#"%d",[request responseStatusCode]);
You need to put something like this:
[request setDidFinishSelector:#selector(didFinishRequest:)];
And then move your NSLogs and any other stuff that leeches off of the response into that method.

ASIHTTPRequest in iOS

I have been using ASIHTTPRequest for an application but it gives me error in topSecretFetchFailed method 5 out of 10 request, Not sure how to deal with it, Isn't ASIHTTPRequest stable enough?
[request setDidFailSelector:#selector(topSecretFetchFailed:)];
EDIT:
This is my code or method which get called in each request. MARKET_INDEXES_URL its static string which has "someurl.com";
- (void)requestData {
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:[NSURLURLWithString:MARKET_INDEXES_URL]];
[req setDelegate:self];
[req setDidFailSelector:#selector(topSecretFetchFailed:)];
[req setDidFinishSelector:#selector(topSecretFetchComplete:)];
[self setRequest:req];
[self.request startAsynchronous];
}
and this is the fail handler
- (void)topSecretFetchFailed:(ASIHTTPRequest *)theRequest {
[[NSNotificationCenter defaultCenter] postNotificationName:#"MarketIndexesError" object:nil];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:#"Warning !" message:#"Connection error, Please try again" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[view show];
[view release];
NSLog(#"MarketIndex service Fail: %d %#", [theRequest responseStatusCode], [theRequest responseStatusMessage]);
}
What you need is some reporting of the response details. Without that, you're diagnosing in the dark. Put this in your failure handler:
NSLog(#"Fail: %d %#", [request responseStatusCode], [request responseStatusMessage]);
ASIHTTPRequest is stable. You are probably getting error because either your network is down or your server is taking too long to respond.
You should try changing the ASIHTTPRequest property numberOfTimesToRetryOnTimeout to something that suits you.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:....
request.numberOfTimesToRetryOnTimeout=10;

ASIHTTPRequest - HTTPS

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.