AFN参数中的特殊字符会导致访问错误

时间:2016-06-24 09:43:38

标签: ios objective-c

NSString *_str=[NSString stringWithFormat:@"28:76:CD:01:45:AE"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameter = @{@"uin" :_str ,
                            @"type" : @"2"
                            };

[manager GET:@"http://www.baidu.com" parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
}];

代码打印如下: ~~~~~~~~~ http://www.baidu.com/?type=2&uin=28%3A76%3ACD%3A01%3A45%3AAE

我通过url中的write参数解决了这个问题:

    [manager GET:@"http://www.baidu.com?uin=28:76:CD:01:45:AE&type=2" parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
      // print: ~~~~~~~~~http://www.google.com/?type=2&uin=28:76:CD:01:45:AE
      NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
  }];

我认为这不是一个好方法,有更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

这是方法之一:

将该值存储在一个String

_str=[[NSString stringWithFormat:@"28:76:CD:01:45:AE"];

然后:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameter = @{@"uin" :_str ,

                            @"type" : @"2"
                           };

[manager GET:@"http://www.google.com" parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
      // print: ~~~~~~~~~http://www.google.com/?type=2&uin=28%3A76%3ACD%3A01%3A45%3AAE
      NSLog(@"~~~~~~~~~%@", task.currentRequest.URL);
  }];