gpt4 book ai didi

iphone - 错误域=AFNetworkingErrorDomain 代码=-1011 "Request failed: method not allowed (405)"

转载 作者:行者123 更新时间:2023-12-03 21:01:12 26 4
gpt4 key购买 nike

我正在使用 AFNetworking 进行 Web 服务。它在 NSURLConnection 上运行良好。使用 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 进行请求。使用下面的代码可能对您有帮助。请让我们知道您的意见。

 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];

关于iphone - 错误域=AFNetworkingErrorDomain 代码=-1011 "Request failed: method not allowed (405)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com