У меня проблема. Я отправляю на сервер POST
запрос из программы (xcode, я разработал на объективе C), данные отправляются, но мне нужно зарегистрироваться на сайте (проверка подлинности), и я не знаю, как я отправил эту строку
NSString * param = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword]
Мой код полностью:
request.HTTPMethod = @"POST";
NSString * myName = _loginLogin.text;
NSString * myPassword= _passwordLogin.text;
NSString * param = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Я работаю с этой страницей * (www.dnevnik.ru) *.
и для моей строки сайта NSString *postString = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];
не подойдет?
Используйте следующий код: Просто добавьте
@property (nonatomic, strong) NSMutableData *responseData;
В файле .h
NSUserDefaults *loginData = [NSUserDefaults standardUserDefaults];
NSString *username = [loginData objectForKey:@"username"] ;
NSString *password = [loginData objectForKey:@"password"];
NSString *postString = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];
NSString *urlString = @"http://YourUrl";
NSURL *myURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *detailRequestToServer =[NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[detailRequestToServer setHTTPMethod:@"POST"];
[detailRequestToServer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
const char *utfString = [postString UTF8String];
NSString *utfStringLenString = [NSString stringWithFormat:@"%zu", strlen(utfString)];
[detailRequestToServer setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]];
[detailRequestToServer setValue:utfStringLenString forHTTPHeaderField:@"Content-Length"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:detailRequestToServer delegate:self];
if (theConnection)
{
self.responseData = [[NSMutableData alloc] init];
}
else
NSLog(@"Connection Failed!");
NSString *bodyData = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]]; //replace your url here
// Set the request content type to application/x-www-form-urlencoded
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData dataWithCapacity: 0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
if (!theConnection) {
// Release the receivedData object.
receivedData = nil;
// Inform the user that the connection failed.
}