发送带有特殊字符iOS的POST请求

时间:2015-03-25 10:43:06

标签: ios objective-c

我的应用程序调用Web服务进行登录。通常服务没有任何问题,但是当我输入带有特殊字符的密码时(例如:!*' \"();:@& = + $,/?%# []%),网络服务器不允许我登录,因为在服务器端我收到的密码没有特殊字符,例如:

在我的应用上插入密码:test+test

密码接收到网络服务器:testtest

如您所见,请求删除特殊字符实际上我使用以下代码发送登录凭据:

- (id)sendRequestToURL:(NSString *)url withMethod:(NSString *)method withUsername:(NSString*)username withPassword:(NSString*)password andInstallationId:(NSString*)installationId {
    NSURL *finalURL = [[NSURL alloc]init];

    if ([method isEqualToString:@"POST"]) {
        finalURL = [NSURL URLWithString:url];
    } else {
        NSLog(@"Metodo no previsto");
    }

    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", username, password, installationId];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)postData.length];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:finalURL];
    [request setHTTPMethod:method];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        [connection start];
    }

    return connection;
}

我如何解决这个问题?我查看了NSString class reference,我看到方法- stringByAddingPercentEncodingWithAllowedCharacters:没有删除特殊字符,是否有人知道如何使用此方法?你能告诉我一个代码片段吗?

谢谢

3 个答案:

答案 0 :(得分:7)

我找到了解决这个问题的方法,下面是我的代码,也许对某人有用:

- (id)sendRequestToURL:(NSString *)url withMethod:(NSString *)method withUsername:(NSString*)username withPassword:(NSString*)password andInstallationId:(NSString*)installationId {
    NSURL *finalURL = [[NSURL alloc]init];

    if ([method isEqualToString:@"POST"]) {
        finalURL = [NSURL URLWithString:url];
    } else {
        NSLog(@"Metodo no previsto");
    }

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"];

    NSString *encodedPassword = [password stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", username, encodedPassword, installationId];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)postData.length];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:finalURL];
    [request setHTTPMethod:method];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        [connection start];
    }

    return connection;
}

通过这种方式,我在密码字符串中搜索特殊字符,如果发现它使用百分比编码更改此字符。这样我就可以使用普通密码和带有特殊字符的密码登录。我希望它对某人有用。

答案 1 :(得分:2)

只需使用- stringByAddingPercentEscapesUsingEncoding:编码每个参数。

NSString *encodeUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodePassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodeInstallationId = [installationId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", encodeUsername, encodePassword, ins encodeInstallationId allationId];

并在您的请求中使用此编码参数。

答案 2 :(得分:-1)

Url将您的字符串和POST编码为Web服务。请使用以下方法进行网址编码。

+(NSString *) urlencode: (NSString *) str {
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                               NULL,
                                                                               (CFStringRef)str,
                                                                               NULL,
                                                                               (CFStringRef)@" ",
                                                                               kCFStringEncodingUTF8 );

return [encodedString autorelease];

}