2012-03-19 97 views
2

我正在制作一个lo-gin应用程序我想发送用户名和密码到服务器进行验证如何做到这一点我已经在很多方面做了,但我是无法发布。 我张贴的用户名和密码,但如果我直接给用户名和密码到PHP它的工作原理,以便如何做到这一点的iphone通过邮局发送如何发送用户名和密码使用发布到iphone服务器中的服务器

NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",username,pass]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSURL *url = [NSURL URLWithString:@"http://www.celeritas-solutions.com/emrapp/connect.php?"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 
theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPBody:postData];  

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if(theConnection) 
{ 
    webData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"Inside the else condition"); 
} 

[nameInput resignFirstResponder]; 
[passInput resignFirstResponder]; 
nameInput.text = nil; 
passInput.text = nil; 
+0

看一看[本S.O文章] [1]介绍了如何使一个POST请求。 [1]:http://stackoverflow.com/questions/3968107/http-post-request – sergio 2012-03-19 09:34:26

+0

我已经看过这个相同的结果 – user1263350 2012-03-19 09:39:24

+0

添加以下代码后 – Rupesh 2012-03-19 09:54:50

回答

1

//编辑你的代码试试这可能对你有帮助。

NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",username,pass]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSURL *url = [NSURL URLWithString:@"http://www.celeritas-solutions.com/emrapp/connect.php?"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPBody:postData]; 
+0

相同它不给任何reposne相同@“” – user1263350 2012-03-19 10:47:36

+0

尝试更改编码:NSData * postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; – Kuldeep 2012-03-19 10:52:41

+0

如何更改编码 – user1263350 2012-03-19 10:53:24

1

添加以下代码

[theRequest setHTTPBody:postData]; 

NSURLResponse *response;// = [[NSURLResponse alloc] init]; 

NSError *error;// = [[NSError alloc] init; 

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 

NSLog(@"Login response: is %@",str); 
后没有工作
2
//when the user clicks login<action>  


    - (IBAction)signinClicked:(id)sender { 
     NSInteger success = 0; 
     @try { 
      //to check if username and password feild are filled 
      if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""]) { 

       [self alertStatus:@"Please enter Username and Password" :@"Sign in Failed!" :0]; 

      } else { 
       NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.txtUsername text],[self.txtPassword text]]; 
       NSLog(@"PostData: %@",post); 
       //post it to your url where your php file is saved for login 
       NSURL *url=[NSURL URLWithString:@"http://xyz.rohandevelopment.com/new.php"]; 

       NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

       NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
       [request setURL:url]; 
       [request setHTTPMethod:@"POST"]; 
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
       [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
       [request setHTTPBody:postData]; 

       //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

       NSError *error = [[NSError alloc] init]; 
       NSHTTPURLResponse *response = nil; 
       NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

       NSLog(@"Response code: %ld", (long)[response statusCode]); 

       if ([response statusCode] >= 200 && [response statusCode] < 300) 
       { 
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
        NSLog(@"Response ==> %@", responseData); 

        NSError *error = nil; 
        NSDictionary *jsonData = [NSJSONSerialization 
               JSONObjectWithData:urlData 
               options:NSJSONReadingMutableContainers 
               error:&error]; 

        success = [jsonData[@"success"] integerValue]; 
        NSLog(@"Success: %ld",(long)success); 

        if(success == 1) 
        { 
         NSLog(@"Login SUCCESS"); 
        } else { 

         NSString *error_msg = (NSString *) jsonData[@"error_message"]; 
         [self alertStatus:error_msg :@"Sign in Failed!" :0]; 
        } 

       } else { 
        //if (error) NSLog(@"Error: %@", error); 
        [self alertStatus:@"Please Check Your Connection" :@"Sign in Failed!" :0]; 
       } 
      } 
     } 
     @catch (NSException * e) { 
      NSLog(@"Exception: %@", e); 
      [self alertStatus:@"Sign in Failed." :@"Error!" :0]; 
     } 
     if (success) { 
      [self performSegueWithIdentifier:@"login_success" sender:self]; 
       } 

    } 






    - (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title 
                  message:msg 
                  delegate:self 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil, nil]; 
     alertView.tag = tag; 
     [alertView show]; 
    }