Ios 使用+;和:签名
我正在使用AFNetworking for iOS,我想发送一个带有查询参数的请求,该参数的值为datetime。想要的行为应该是:Ios 使用+;和:签名,ios,swift,datetime,afnetworking,url-encoding,Ios,Swift,Datetime,Afnetworking,Url Encoding,我正在使用AFNetworking for iOS,我想发送一个带有查询参数的请求,该参数的值为datetime。想要的行为应该是: Original: 2016-07-04T14:30:21+0200 Encoded: 2016-07-04T14%3A30%3A21%2B0200 Example: .../?datetime=2016-07-04T14%3A30%3A21%2B0200 AFNetworking本身进行字符串编码,不包括特殊字符,如+/&:和其他一些(),这很好,因为它们
Original: 2016-07-04T14:30:21+0200
Encoded: 2016-07-04T14%3A30%3A21%2B0200
Example: .../?datetime=2016-07-04T14%3A30%3A21%2B0200
AFNetworking本身进行字符串编码,不包括特殊字符,如+/&:
和其他一些(),这很好,因为它们是保留的。
因此,我必须以另一种方式对datetime的值进行编码,以避开加号和冒号。但是,当我在开始之前手动编码值时,它显然会逃逸%
两次。因此,它为每个%
放置一个%25
2016-07-04T14%253A30%253A21%252B0200
我希望对查询使用百分比编码,并使用允许的字符,如:
query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())
我没有找到一个解决方案来更改或禁用AFNetworking的编码以完全手动完成。你有什么建议吗?经过进一步的研究,我找到了一个地方来注入我想要的编码。这就是它不起作用的方式: 编码不起作用 初始化
请求操作管理器
:
self.requestOperationManager = [[AFHTTPRequestOperationManager alloc] init];
self.requestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];
使用requestOperationManager
初始化操作
NSURLRequest *request = [NSURLRequest alloc] initWithURL:url]; // The problem is here
AFHTTPRequestOperation *operation = [self.requestOperationManager HTTPRequestOperationWithRequest:urlRequest success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Failure
}];
[self.requestOperationManager.operationQueue addOperation:operation];
[operation start];
拥有更多控制权的方法
AFHTTPRequestSerializer
还可以创建请求,您可以使用自己的序列化
初始化requestOperationManager
并添加查询字符串序列化块:
self.requestOperationManager = [[AFHTTPRequestOperationManager alloc] init];
self.requestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];
[self.requestOperationManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
if ([parameters isKindOfClass:[NSString class]]) {
NSString *yourEncodedParameterString = // What every you want to do with it.
return yourEncodedParameterString;
}
return parameters;
}];
现在更改创建NSURLRequest
的方式:
NSString *method = @"GET";
NSString *urlStringWithoutQuery = @"http://example.com/";
NSString *query = @"datetime=2016-07-06T12:15:42+0200"
NSMutableURLRequest *urlRequest = [self.requestOperationManager.requestSerializer requestWithMethod:method URLString:urlStringWithoutQuery parameters:query error:nil];
将url拆分是非常重要的。在不查询URLString
参数的情况下使用url,并且只查询参数
参数。通过使用requestWithMethod:URLString:parameters:error
它将调用上面提供的查询字符串序列化块,并根据需要对参数进行编码
AFHTTPRequestOperation *operation = [self.requestOperationManager HTTPRequestOperationWithRequest:urlRequest success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Failure
}];
[self.requestOperationManager.operationQueue addOperation:operation];
[operation start];