0

我为应用程序制作了一个登录页面。凭据详细信息存储在数据库中。所以我正在制作一个 ASP.Net 中间件服务。

现在,以前我正在使用 GET 方法发送请求,该请求在 URL STRING 中附加详细信息(我现在不想要)。我想做 POST 方法。所以我找到了一个非常好的链接,但我不确定我是否正确地完成了它,或者的 ASP.net 同事犯了一些错误,因为它不起作用

如果凭据(即用户名和密码)正确,则服务会向我返回这样的 XML

<result>
    success
</result>

否则失败

那么任何人都可以告诉我这段代码是否正确,如果不是,那是什么错误......谢谢您的时间。

代码: -

@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

如果我缺少任何要发布的内容,请告诉我。:))

4

2 回答 2

1

您已经创建了请求,但您没有将其发送到任何地方。
使用您的请求实例化一个NSURLConnection对象 - 这会将其发送到服务器并将结果报告回它的委托。

于 2011-12-14T14:43:36.923 回答
0

最后我得到了我的代码,因为通常我犯了一个非常基本的错误......我没有给解析器正确的数据来解析......这是所有工作正常和良好的代码。

代码 :-

@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

所以享受代码并玩得开心.... :))

于 2011-12-15T06:08:32.913 回答