Reputation: 2514
I making an Login Page for an app. the Credential details are stored in a DB. so i am making an ASP.Net middleware service.
Now previously i was doing GET method to send request which appends the details in URL STRING(which i dont want now).I want to do the POST method. so i found one Very good link for that, but i am not sure that whether i have done it correctly or not OR my ASP.net Colleague has done some mistake as it is not working.
If the Credentials i.e Username and Password are correct the Service returns me an XML like this
<result>
success
</result>
or failure if not.
So can any one please tell me that whether this code is correct or not and if not what is the Mistake.... Thanks For your time.
Code: -
@class FirstViewController;
@interface TestLoginViewController : UIViewController<UITextFieldDelegate,NSXMLParserDelegate>{
IBOutlet UITextField *txtUserName,*txtPassword;
IBOutlet UIButton *submitDetails;
FirstViewController *viewController;
NSString *currentElement,*status;
NSString *loginName,*password;
}
@property (nonatomic,retain) IBOutlet UITextField *txtUserName;
@property (nonatomic,retain) IBOutlet UITextField *txtPassword;
@property (retain) NSString *loginName;
@property (retain) NSString *password;
@property (retain) NSString *status;
-(IBAction)onTapSubmit:(id)sender;
-(IBAction)onTapReset;
@end
-(IBAction)onTapSubmit:(id)sender{
NSLog(@"UserName :- %@",txtUserName.text);
NSLog(@"Password :- %@",txtPassword.text);
if(![txtUserName.text isEqualToString:@""] && ![txtPassword.text isEqualToString:@""]){
// NSString *uName = txtUserName.text;
// NSString *uPass = txtPassword.text;
// NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",[self urlEncodeValue:uName],[self urlEncodeValue:uPass]];
NSString *temp1 = [@"username=" stringByAppendingString:txtUserName.text];
NSString *temp2 = [temp1 stringByAppendingString:@"&password="];
NSString *post = [temp2 stringByAppendingString:txtPassword.text];
NSLog(@"Post String ==== %@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://iphonewebserver.wsisites.net/Default.aspx"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSXMLParser *parser= [[NSXMLParser alloc]initWithData:postData];
**EDITED :-**
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(@"%@", outputdata);
**//Nslog output
Succeeded! Received 39 bytes of data
2011-12-14 21:28:21.461 TestLogin[1094:207] <result><login>success</login></result>
2011-12-14 21:28:21.463 TestLogin[1094:207] Status======= (null)**
/*
NSMutableString *strURL=[[NSMutableString alloc] init];
[strURL appendFormat:@"http://iphonewebserver.wsisites.net/Default.aspx?username="];
[strURL appendFormat:@"%@",txtUserName.text];
[strURL appendFormat:@"&password="];
[strURL appendFormat:@"%@",txtPassword.text];
NSLog(@"urlformed:-%@",strURL);
NSURL *url= [NSURL URLWithString:strURL];
NSData *data= [NSData dataWithContentsOfURL:url];
[strURL release];
NSXMLParser *parser= [[NSXMLParser alloc]initWithData:data];
*/
parser.delegate=self;
[parser parse];
[parser release];
// if([self.status isEqualToString:@"success"]){
if(self.status){
viewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
[self presentModalViewController:viewController animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed !!! " message:@"Please Enter Correct Username and Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"%@",self.status);
}
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed !!! " message:@"Please Enter the Username and Password" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)
{
[self onTapReset];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
txtUserName.delegate = self;
txtPassword.delegate = self;
txtUserName.text = @"admin";
txtPassword.text = @"pass";
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = elementName;
if([elementName isEqualToString:@"result"]) {
NSLog(@"%@",currentElement);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString:@"login"]){
self.status=[NSString stringWithFormat:@"%@",string];
NSLog(@"%@",self.status);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
currentElement=@"";//required to reset current element
}
@end
If anything i am missing to post please tell me. :))
Upvotes: 0
Views: 223
Reputation: 2514
Finally i got my code works, as usually i was doing a very Basic mistake.... I was not giving the parser correct data to parse.... this is the code all working fine and Good.
CODE :-
@class FirstViewController;
@interface TestLoginViewController : UIViewController<UITextFieldDelegate,NSXMLParserDelegate>{
IBOutlet UITextField *txtUserName,*txtPassword;
IBOutlet UIButton *submitDetails;
FirstViewController *viewController;
NSString *currentElement,*status;
NSString *loginName,*password;
}
@property (nonatomic,retain) IBOutlet UITextField *txtUserName;
@property (nonatomic,retain) IBOutlet UITextField *txtPassword;
@property (retain) NSString *loginName;
@property (retain) NSString *password;
@property (retain) NSString *status;
-(IBAction)onTapSubmit:(id)sender;
-(IBAction)onTapReset;
@end
-(IBAction)onTapSubmit:(id)sender{
NSLog(@"UserName :- %@",txtUserName.text);
NSLog(@"Password :- %@",txtPassword.text);
if(![txtUserName.text isEqualToString:@""] && ![txtPassword.text isEqualToString:@""]){
NSString *temp1 = [@"username=" stringByAppendingString:txtUserName.text];
NSString *temp2 = [temp1 stringByAppendingString:@"&password="];
NSString *post = [temp2 stringByAppendingString:txtPassword.text];
NSLog(@"Post String ==== %@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://iphonewebserver.wsisites.net/Default.aspx"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(@"%@", outputdata);
NSXMLParser *parser= [[NSXMLParser alloc]initWithData:urlData];
parser.delegate=self;
[parser parse];
[parser release];
if([self.status isEqualToString:@"success"]){
viewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
[self presentModalViewController:viewController animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed !!! " message:@"Please Enter Correct Username and Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"%@",self.status);
}
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed !!! " message:@"Please Enter the Username and Password" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)
{
[self onTapReset];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
txtUserName.delegate = self;
txtPassword.delegate = self;
txtUserName.text = @"admin";
txtPassword.text = @"pass";
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = elementName;
if([elementName isEqualToString:@"result"]) {
NSLog(@"%@",currentElement);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString:@"login"]){
self.status=[NSString stringWithFormat:@"%@",string];
NSLog(@"%@",self.status);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
currentElement=@"";//required to reset current element
}
@end
So Enjoy the Code and have fun.... :))
Upvotes: 0
Reputation: 12106
You have created the request, but you're not sending it anywhere.
Instantiate a NSURLConnection object using your request - that will send it to the server and report the results back to it's delegate.
Upvotes: 1