我必须登录我的网络服务器。一切正常,但没有生成密码,如|%<“> {}¥^〜。我怎么编码这样的密码?
我创建一个用户密码= |%<“> {}¥^〜不起作用(例如像”user1234“这样的密码工作正常)
NSString *userName = self.usernameOutlet.text;
NSString *userPassword = self.passwordOutlet.text;
NSString *escapedString = [self.passwordOutlet.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
userPassword = escapedString;
NSString *post = [NSString stringWithFormat:@"login=%@&password=%@",userName,userPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://XXXXXXXX/login"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
使用像“user1234”这样的密码我得到一个cookie
密码如“|%<”> {}¥^〜“我没有cookie
我究竟做错了什么?
想要使用URLQueryAllowedCharacterSet
很诱人,但这对所有字符串都不起作用。值得注意的是&
和+
将无法通过。
如果你想知道我们为什么必须逃脱&
和+
的百分比,那是因为这两个字符在x-www-form-urlencoded
请求中具有特殊含义。 &
用于在x-www-form-urlencoded
请求中分隔键值对,因此它会截断您的密码。并且大多数Web服务将+
转换为空间,因此您也希望百分之百逃脱。
所以,让我们首先定义一个可以工作的字符集:
// NSCharacterSet+URLQueryValueAllowed.h
@interface NSCharacterSet (URLQueryValueAllowed)
@property (class, readonly, copy) NSCharacterSet *URLQueryValueAllowedCharacterSet;
@end
和
// NSCharacterSet+URLQueryValueAllowed.m
@implementation NSCharacterSet (URLQueryValueAllowed)
+ (NSCharacterSet *)URLQueryValueAllowedCharacterSet {
static dispatch_once_t onceToken;
static NSCharacterSet *queryValueAllowed;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *allowed = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
NSString *generalDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
NSString *subDelimitersToEncode = @"!$&'()*+,;=";
[allowed removeCharactersInString:generalDelimitersToEncode];
[allowed removeCharactersInString:subDelimitersToEncode];
queryValueAllowed = [allowed copy];
});
return queryValueAllowed;
}
@end
然后,为了让我们的生活更轻松,让我们为编码字典的百分比定义NSDictionary
类别:
// NSDictionary+PercentEncoded.h
@interface NSDictionary (PercentEncoded)
- (NSString *)percentEncodedString;
- (NSData *)percentEncodedData;
@end
和
// NSDictionary+PercentEncoded.m
@implementation NSDictionary (PercentEncoded)
- (NSString *)percentEncodedString {
NSMutableArray<NSString *> *results = [NSMutableArray array];
NSCharacterSet *allowed = [NSCharacterSet URLQueryValueAllowedCharacterSet];
for (NSString *key in self.allKeys) {
NSString *encodedKey = [key stringByAddingPercentEncodingWithAllowedCharacters:allowed];
NSString *value = [[self objectForKey:key] description];
NSString *encodedValue = [value stringByAddingPercentEncodingWithAllowedCharacters:allowed];
[results addObject:[NSString stringWithFormat:@"%@=%@", encodedKey, encodedValue]];
}
return [results componentsJoinedByString:@"&"];
}
- (NSData *)percentEncodedData {
return [[self percentEncodedString] dataUsingEncoding:NSUTF8StringEncoding];
}
@end
然后,您的应用程序代码可以:
NSDictionary *dictionary = @{@"login": userName, @"password": userPassword};
NSData *body = [dictionary percentEncodedData];