MKNetworkKit GET请求中使用中文

在开发app时,按惯例认为用户名是英文和数字,没考虑中文用户名,导致老中文用户名直接闪退,查看代码才发现,使用MKNetworkEngine没有考虑到中文的url转码。

NSString* l_strUrl = [NSString stringWithFormat:@"user.php?a=login&user=%@&pw=%@",m_TFName.text,m_TFPass.text];
NSString *l_url = [l_strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:ServerDomainName2 apiPath:ServerApiPath customHeaderFields:nil];

MKNetworkOperation *op = [engine operationWithPath:l_url params:nil httpMethod:@"GET" ssl:NO];
DDLogVerbose(@"%s %@", __FUNCTION__, l_strUrl);
[op addCompletionHandler:^(MKNetworkOperation *operation) {
[self requestFinished:operation];
}errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
[self requestFailed:errorOp];
}];
[engine enqueueOperation:op];

中文直接使用stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding方法来进行url转码

你可能感兴趣的:(IOS)