ASIHTTPRequest login page - iphone

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.

Related

NSURLConnection error not working

I am trying to detect if there is an error in my request using the if statement on theConnection. It enters the first part if successful fine but does not enter the else if there is an error. I am not sure why.
- (void)vehicleSearchRequest:(NSData *)postBodyData
{
NSString *address = [NSString stringWithFormat:#"http://%#", serverAddress];
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithFormat:#"%#", address];
NSURL *url = [NSURL URLWithString:databaseURL];
NSString *postLength = [NSString stringWithFormat:#"%d", [postBodyData length]];
//SynchronousRequest to grab the data, also setting up the cachePolicy
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //if request dose not finish happen within 60 second timeout.
// Set up request
[request setHTTPMethod: #"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/octet-stream" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];
[request setTimeoutInterval:180];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
// do animation thankyou here
NSLog(#"Sucsess!");
[self submitSuccessful];
} else {
// Inform the user that the connection failed from the connection:didFailWithError method
NSLog(#"Connectin ERROR!!!");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection error, Please try again" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Your code checks to see whether it was able to instantiate an NSURLConnection or not. This is a useful safety check, but will only fail in practice if you pass it a URL that the system does not support.
The connection itself is asynchronous, and so will take a little while to tell you if there was a problem or not. Implement its delegate methods to hear back from the server.

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

How to send POST value to website?

I need to submit value from my application via UITextField and I want this value to show on website that i sent request to. I use ASIHTTPRequest to send request to website. i tried something like this:
NSURL *url = [NSURL URLWithString:#"http://www.project4hire.com/freelance_job_16265.html"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
//
[request setPostValue:priceField forKey:#"bid"];
[request setPostValue:dayField forKey:#"days2c"];
[request setPostValue:commentField forKey:#"comment"];
[request setPostValue:#"1" forKey:#"notify"];
[request setPostValue:#"placebid" forKey:#"Place Bid >>"];
[request setPostValue:#"e6fb12104854e6e9" forKey:#"suid"];
[request setPostValue:#"placebid" forKey:#"a"];
[request setPostValue:#"16265" forKey:#"pid"];
[request setDelegate:self];
[request setDidFailSelector:#selector(requestBidFailed:)];
[request setDidFinishSelector:#selector(requestBidFinished:)];
[request startAsynchronous];
}
- (void)requestBidFailed:(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)requestBidFinished:(ASIHTTPRequest *)request
{
NSLog(#"Status: %d", request.responseStatusCode);
NSLog(#"string: %#",request.responseString);
}
Here are the Bid Form:BidForm
Here are request and response header:Header
I got response 200, but the value that i sent not show on the website. Can anyone advice me?
Thanks
I noticed you are posting to a HTML file. Unless you have some special setup to allow The HTML files to be executable, the html file will not process the data you posted to it. Is it that only one value is not showing or are all values not showing. If all values are missing, then what I stated initially is correct and you will need to use something like PHP, CF, Perl or whatever language you want to receive the data you are posting from your app.
I had the same problem, but mine was working on simulator but it was not working on device then I read an article that it shows the founders of the ASIHTTPRequest API no longer update their library (I dont know if that article was reliable or not), so I decided to use an updated library which is RestKit. You can download and set it up from this website: restkit.org, if you have any problem for installing you can ask me to help you. here are the simple code for posting on restkit library:
- (void)post
{
[RKClient clientWithBaseURLString:#"http://www.project4hire.com"];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
priceField, #"bid",
dayField, #"days2c", nil];
[[RKClient sharedClient] post:#"/freelance_job_16265.html" params:params delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSRange range = [[error localizedDescription] rangeOfString:#"-1012"];
if (range.length > 0){
//Do whatever here to handle authentication failures
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error sending request to the server" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
RKLogError(#"Hit error: %#", error);
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(#"Retrieved XML: %#", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isJSON]) {
NSLog(#"Got a JSON response back from our POST!");
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(#"The resource path '%#' was not found.", [request resourcePath]);
}
}
NSLog(#"HTTP status code: %d", response.statusCode);
NSLog(#"HTTP status message: %#", [response localizedStatusCodeString]);
NSLog(#"Header fields: %#", response.allHeaderFields);
NSLog(#"Body: %#", response.bodyAsString);
}

ASI HTTP Request for posting a review

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.

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;