Hello everyone..I'm trying to send username and password to server side using POST method,but I don't know how to do that. Can anybody suggest me how to do it?
Here is my code..
-(void) alertStatus:(NSString*)msg : (NSString*)title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
-(IBAction) loginAction:(UIButton*)sender
{
NSString*username = _userNameField.text;
NSString*password = _passwordField.text;
NSString*post = [NSString stringWithFormat:@"Username=%@&Password=%@" ,username,password];
NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString*postLength = [NSString stringWithFormat:@"%d" ,[postData length]];
NSMutableURLRequest*request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.31.144.227:8080/rest/login/post"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSError*error = [[NSError alloc] init];
NSHTTPURLResponse*response = nil;
NSData*urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSData*responseData = [[NSData alloc]initWithData:responseData options: NSJSONReadingMutableContainers error:nil];
if([jsonObject objectForKey:@"error"])
{
[self alertStatus:@" " :@" "];
}else{
[self alertStatus:@" " :@" "];
}
}else{
if (error) NSLog(@"Error: %@", error); //Here showing erroe after compilation[Thread1: signal SIGKILL] and [Thread 1:EXC_BAD_ACCESS(code =1,address=0xd)
[self alertStatus:@"Connection Failed!" :@"Login Failed!"];
}
}
After running this i received these following errors:-
-> 0xee8f25 <+165>: calll 0x1092168 ; symbol stub for: getpid [Thread 1:EXC_BREAKPOINT(code=EXC_I386_BPT,subcode=0x0)