错误域= AFNetworkingErrorDomain代码= -1011“请求失败:方法不被允许(405)". [英] Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: method not allowed (405)"

查看:72
本文介绍了错误域= AFNetworkingErrorDomain代码= -1011“请求失败:方法不被允许(405)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 AFNetworking 用于Web服务.在 NSURLConnection 上运行良好.使用 AFNetworking 会出错.

I am using AFNetworking for Web services. It is working fine on NSURLConnection. It gives error using AFNetworking.

这是我的代码:

 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Succes");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

这是我收到的错误:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: method not allowed (405)" UserInfo=0x1e86b390 {NSErrorFailingURLKey=http://..............url.........., AFNetworkingOperationFailingURLResponseErrorKey=, NSLocalizedDescription=Request failed: method not allowed (405)}.

可以帮忙吗?

推荐答案

如果您的API是RestFul,那么您想使用http请求.使用下面的代码可能对您有所帮助.请让我们知道您的评论.

If your API is RestFul then you want to request using http. Use Below code may be it's helpful for you. Please let us know your comments.

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];

NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];

//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {

     NSString *response = [operation responseString];
     NSLog(@"response: %@",response);

 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"error: %@", [operation error]);
 }];

//call start on your request operation
[operation start];

这篇关于错误域= AFNetworkingErrorDomain代码= -1011“请求失败:方法不被允许(405)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆