MaZaHaKa
MaZaHaKa

Reputation: 13

Authentication error on the server

This my code:

#import "MainClass.h"
@implementation MainClass

- (IBAction)actionButton:(id)sender {
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                        URLWithString:@"https://login.dnevnik.ru/auth"]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    request.HTTPMethod = @"POST";

    NSString *username = _loginLogin.text;
    NSString *password= _passwordLogin.text;
    GlobalUserName = username;
    GlobalPassword = password;

    /*NSUserDefaults *loginData = [NSUserDefaults standardUserDefaults];
    NSString *username1 = [loginData objectForKey:@"username"] ;
    NSString *password1 = [loginData objectForKey:@"password"];*/


    NSString * param = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];
    request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    if (connection) {
        _otvet.text = @"Connection setup";
        NSLog(@"ama ama");

    }
    else
    {
        _otvet.text=@"Problem with connection";
        NSLog(@"ama ama faza");
    }
    }
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Ошибка" message:@"Problem with intrnet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString * data = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    if ([data  isEqual: @"OK"]) {

        _otvet.text = @"Yeah";
        NSLog(@"Yeah");
    }
    else
    {
        NSLog(@"Error %@,%@",GlobalUserName,GlobalPassword);
  }
}

Show Message,what NSLog(@"Error %@,%@",GlobalUserName,GlobalPassword) because method isEqual say what my data are not the same.What should i do?and I'm not sure that sending the right site login and password. Tell me how to determine where(whither) to send the data, I don't have site! Website www.dnevnik.ru!!

Upvotes: 0

Views: 74

Answers (1)

CouchDeveloper
CouchDeveloper

Reputation: 19116

You need to setup the parameter string properly.

Here in your code

NSString * param = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];

This looks like you want to use content type application/x-www-form-urlencoded. However, the parameter string starts with a "&" - and this is definitely an error.

When using parameters like this with a content type application/x-www-form-urlencoded, I would suggest to create your parameters as (unencoded) NSString key/value pairs, create a NSDictionary and use the following helper method described in the answer here (API POST method not saving data in sql server) which creates a properly encoded parameter string which you can add to the body.

Don't forget to set the "Content-Type" header to application/x-www-form-urlencoded.

Upvotes: 1

Related Questions