Нет данных с NSURLSession
Мой почтовый запрос работает нормально (у меня хороший ответ и нет ошибок), но я тоже не получаю данные. Обычно, когда я нахожусь в своем браузере на этом сайте https://conventiondestage.insa-toulouse.fr/index.php Я заполните форму и после входа в систему я вижу содержимое страницы accueil.php, но почему моя функция не возвращает данные (я хочу html-содержимое этой страницы)?
Спасибо за вашу помощь!
Это лог в консоли:
2014-04-07 17:52:00.207 myINSA[22797:60b] Response:<NSHTTPURLResponse: 0x14e92d50> { URL: https://conventiondestage.insa-toulouse.fr/accueil.php } { status code: 200, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate";
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 1428;
"Content-Type" = "text/html";
Date = "Mon, 07 Apr 2014 15:52:00 GMT";
Expires = "Mon, 26 Jul 1997 05:00:00 GMT";
"Keep-Alive" = "timeout=15, max=100";
Pragma = "no-cache";
Server = "Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny16 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g";
Vary = "Accept-Encoding";
"X-Powered-By" = "PHP/5.2.6-1+lenny16";
} } (null)
2014-04-07 17:52:00.210 myINSA[22797:60b] Data = (null)
Это моя функция:
- (IBAction)pushLogin:(id)sender {
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"https://conventiondestage.insa-toulouse.fr/index.php"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"login=myLogin&password=myPass";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"max-age=0" forHTTPHeaderField:@"Cache-Control"];
[urlRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[urlRequest addValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
[urlRequest addValue:@"https://conventiondestage.insa-toulouse.fr" forHTTPHeaderField:@"Origin"];
[urlRequest addValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36" forHTTPHeaderField:@"User-Agent"];
[urlRequest addValue:@"gzip,deflate,sdch" forHTTPHeaderField:@"Accept-Encoding"];
[urlRequest addValue:@"https://conventiondestage.insa-toulouse.fr/index.php" forHTTPHeaderField:@"Referer"];
[urlRequest addValue:@"conventiondestage.insa-toulouse.fr" forHTTPHeaderField:@"Host"];
[urlRequest addValue:@"fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4" forHTTPHeaderField:@"Accept-Language"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString *text = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
}
У меня такой же результат с этим кодом:
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://conventiondestage.insa-toulouse.fr/index.php"]];
[request setHTTPMethod:@"POST"];
NSString *post =@"login=mylogin&password=mypass";
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"Data = %@",[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding]);