JSON请求问题,NSString属性

时间:2014-05-02 00:32:35

标签: ios objective-c json nsmutableurlrequest

我遇到了一些困难,正在为我正在处理的后端提出请求。例如,我之前使用用户名创建的用户:“test23”及其密码:“ws0ei”在制作此JSON请求时非常有效:

self.username = @"test23";
self.password = @"ws0ei";

NSDictionary *jsonDictionary = @{@"action" : @"upload_profile_image",
                                    @"username" : self.username,
                                    @"password_hash" : [self md5HexDigest:self.password]};

NSString *requestString = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:jsonDictionary options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];

NSString *escaped = [[NSString stringWithFormat:@"http://www.host.com/server.php?request=%@",requestString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escaped]];

- (NSString*)md5HexDigest:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];

    for (int i = 0; i<CC_MD5_DIGEST_LENGTH; i++)
    {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

但是,当使用我创建的“test32”用户名和相同的密码“ws0ei”测试相同的请求时,API会以

响应
{
    reason = "Invalid username/password";
    status = "image not uploaded";
}

我不知道问题是什么,我认为它与不正确的编码有关,或者与应用程序端的md5有关,而不是后端。

我很感激提供任何帮助。谢谢。 编辑:包括用户“test23”JSON字典,请求网址和请求响应的一些控制台日志,以及用户“test32”以向您显示它们是相同的“

用户名:test23

JSON字典

2014-05-01 20:53:51.311 test[20603:60b] 
{
    action = "upload_profile_image";
    "password_hash" = 19144b5589ca801f459fe42db9ec859a;
    username = test23;
}

请求网址(转义)

2014-05-01 20:53:51.312 test[20603:60b] http://www.host.com/server.php?request=%7B%22username%22:%22test23%22,%22action%22:%22upload_profile_image%22,%22password_hash%22:%2219144b5589ca801f459fe42db9ec859a%22%7D

响应

2014-05-01 20:53:51.850 test[20603:60b] 
{
    status = "image uploaded";
}

用户名:test32

JSON字典

2014-05-01 20:57:00.988 test[20625:60b] 
{
    action = "upload_profile_image";
    "password_hash" = 19144b5589ca801f459fe42db9ec859a;
    username = test32;
}

请求网址(转义)

2014-05-01 20:57:00.989 test[20625:60b] http://www.host.com/server.php?request=%7B%22username%22:%22test32%22,%22action%22:%22upload_profile_image%22,%22password_hash%22:%2219144b5589ca801f459fe42db9ec859a%22%7D

响应

2014-05-01 20:57:01.464 test[20625:60b] 
{
    reason = "Invalid username/password";
    status = "image not uploaded";
}

1 个答案:

答案 0 :(得分:0)

事实证明这确实是一个后端问题。问题现在已经解决了