MaZaHaKa
MaZaHaKa

Reputation: 13

POST Request with NSDictionary

Hi Guys :D Please explain to me how I should use in your class NSDictionary (on this page).What i should say request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; that my program work correctly?

This my code

#import "MainClass.h"

@implementation MainClass

- (IBAction)actionButton:(id)sender {

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                    URLWithString:@"https://login.dnevnik.ru"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];


request.HTTPMethod = @"POST";
NSString *username = _loginLogin.text;
NSString *password= _passwordLogin.text;
GlobalUserName = username;
GlobalPassword = password;
NSDictionary *param = @{@"Username": GlobalUserName,@"password": GlobalPassword};



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


//Параметры посылаемого запроса
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8"
forHTTPHeaderField:@"Content-Type"];


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


if (connection) {
    _otvet.text = @"Соединение установлено";
    NSLog(@"ama ama");

}
else
{
    _otvet.text=@"Проблема с соединением";
    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:@"Пожалуйста, проверьте
соединение с Интернетом." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];          
}


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

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

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

    _otvet.text = @"Вы успешно залогинены.";
    NSLog(@"Вы успешно залогинены.");
}
else
{
    NSLog(@"Ошибка авторизации %@,%@",GlobalUserName,GlobalPassword);
}
}

In my case xcode when compiling says

No visible @interface for 'NSDictionary' declares the selector 'dataUsingEncoding:'

Upvotes: 1

Views: 949

Answers (1)

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19802

You should pass NSString as a BODY.

Do this:

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

Or if you want encode NSDictionary to JSON String and decode it on backend - better way to do that, but more work :)

request.HTTPBody = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&error]

Upvotes: 2

Related Questions