我在下面的代码中使用了一个名为ASIHTTPRequest的旧库。我正在寻找升级库,但需要一些帮助,以确保我正确地做到这一点并让我的脚湿透。这是我需要转换为AFNetworking的代码。任何提示或建议表示赞赏。
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setDelegate:self];
NSMutableDictionary *userinfo = [[NSMutableDictionary alloc] init];
[userinfo setObject:NSStringFromSelector(self.didFinishSelector) forKey:@"didFinishSelector"];
[userinfo setObject:NSStringFromSelector(self.didFailSelector) forKey:@"didFailSelector"];
[request setUserInfo:userinfo];
[request setUserAgentString:USER_AGENT];
[request setTimeOutSeconds:60];
[request addRequestHeader:@"apiKey" value:apiKey];
[request setRequestMethod:method];
答案 0 :(得分:0)
使用AFNetworking
代码应如下所示
NSDictionary *parameters=@{"key":value};//The values you want to POST arrange in dictionary.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:apiKey forHTTPHeaderField:@"apiKey"];
[manager.requestSerializer setValue:USER_AGENT forHTTPHeaderField:@"User-Agent"];
[manager.requestSerializer setTimeoutInterval:60];
[manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Result %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.description);
}];
我希望它有所帮助。欢呼声。