2012-03-19 96 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]; 
    }