我用NSData对象做了一个NSString:
NSData* data = request.HTTPBody;
NSString* s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];现在我有了NSString,它为我提供了以下NSLog:
WebKitFormBoundaryA7rpc3udSxxsvBFm
Content-Disposition: form-data; name="username"
Testusernamevalue
------WebKitFormBoundaryA7rpc3udSxxsvBFm
Content-Disposition: form-data; name="password"
Testpasswordvalue
------WebKitFormBoundaryA7rpc3udSxxsvBFm--如何将“用户名”和“密码”这两个值保存到用户名和密码的单个NSString中?
谢谢你的帮助。向Eddi致以最美好的问候
发布于 2017-03-06 00:34:56
我想我理解这个问题的意思是,我们需要"Content-Disposition“行后面的行上的字符串值。为此,我们需要遍历这些行,保持查找定界行及其类型(用户名或密码)的状态
NSArray *components = [theLongContentDispositionString componentsSeparatedByString:@"\n"];
NSMutableDictionary *values = [NSMutableDictionary dictionary];
NSString *key = nil;
for (NSString *line in components) {
if (key && line.length) {
values[key] = line;
key = nil;
} else if ([line hasPrefix:@"Content-Disposition"]) {
// does the line contain "username"? Assume password if not
NSRange r = [line rangeOfString:@"username"];
key = (r.location == NSNotFound)? @"password" : @"username";
}
}值字典最终应该如下所示:
{
password = "non-empty line after Content-Disposition line with password";
username = "non-empty line after Content-Disposition line with username";
}https://stackoverflow.com/questions/42610397
复制相似问题