Iphone - AFNetworking登录表单始终返回404

时间:2013-06-10 15:23:39

标签: iphone forms login afnetworking

我遇到了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:method

进行登录请求
[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);    
}];

这两种方法都不起作用,我猜我在某处做错了什么

btw,我登录的服务器有一个未签名的ssl证书,但是我通过添加

来处理错误
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1 
AFURLConnectionOperation.h中的

我使用Charles来监控http请求,它给了我一个“SSLHandshake:握手期间远程主机关闭连接”

我的应用程序使用iOS 5运行。

任何人都有这方面的见解吗?

利奥

2 个答案:

答案 0 :(得分:1)

我的解决方案是使用空网址初始化我的请求,然后在postPath中输入整个地址。

答案 1 :(得分:0)

您需要在设备上安装Charles的SSL证书才能解密SSL流量

http://www.charlesproxy.com/documentation/faqs/ssl-connections-from-within-iphone-applications/