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);
}];
そして、これら2つの方法のいずれも機能していません。どこかで何か間違ったことをしていると思います
ところで、私がログインするサーバーには署名されていないssl証明書がありますが、追加してエラーを処理しました
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1
AFURLConnectionOperation.h 内
Charles を使用して http リクエストを監視すると、「SSLHandshake: ハンドシェイク中にリモート ホストが接続を閉じました」というメッセージが表示されます。
私のアプリは iOS 5 で動作します。
誰でもこれについて洞察がありますか?
レオ