我在使用 AFNetworking 时遇到问题,决定使用它,因为 ASIHTTP 早已被弃用,我正尝试在登录表单请求中使用它,但响应代码始终为 -1011。
这是我的代码:
NSString *urltest = [NSString stringWithFormat:@"%@%@/", SERVER_ADDRESS, API_ADDRESS];
NSURL *url = [NSURL URLWithString:urltest];
NSLog(@"url address : %@",url);
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.allowsInvalidSSLCertificate = TRUE;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
_email.text, @"email",
_password.text, @"password",
nil];
// 这里我尝试使用 AFHTTPRequestOperation 进行登录请求
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/auth/login" parameters:params];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:@"Content-Type"];
//Notice the different method here!
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}];
//Enqueue it instead of just starting it.
[httpClient enqueueHTTPRequestOperation:operation];
// 这里我尝试使用 postPath:parameters: 方法进行登录请求
[httpClient postPath:@"/auth/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self performSegueWithIdentifier:@"AuthOK" sender:self];
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request failed with code %d for %@ for request : %@",error.code , error.localizedDescription,httpClient.baseURL);
}];
这两种方法都不起作用,我猜我在某处做错了什么
顺便说一句,我登录的服务器有一个未签名的 ssl 证书,但我通过添加来处理错误
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1
在 AFURLConnectionOperation.h
我使用 Charles 来监控 http 请求,它给了我一个“SSLHandshake:握手期间远程主机关闭连接”
我的应用程序在 iOS 5 上运行。
有人对此有见解吗?
狮子座