objective-c - iOS JSON 身份验证 token 请求显示缺少参数

标签 objective-c ios json

我查遍了一切,似乎无法弄清楚这一点。 我看到的 JSON 示例中没有一个在 url 中包含方法名称,因此我认为这可能是一个问题。 这是我的代码:

NSString *username = @"xxxx";
NSString *password = @"xxxx";
NSString *loginURL = @"http://www.xxxxxx.com/services/api/rest/json?method=xxxxxx.auth.gettoken";

NSURL *url = [NSURL URLWithString:loginURL];

NSString *JSONString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];

NSData *JSONBody = [JSONString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = @"POST";
loginRequest.HTTPBody = JSONBody;

NSOperationQueue *queue = [NSOperationQueue new];

[NSURLConnection sendAsynchronousRequest:loginRequest 
                                   queue:queue 
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

                           // Manage the response here.
                           NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
                           NSLog(@"%@", txt);
                       }];

我正在使用 SquidMan 监听请求,我注意到访问日志列出了: www.xxxxx.com/services/api/rest/json? - 直接的/..... 方法名称不存在,这是我第一次使用 SquidMan 执行此操作,但我假设它会存在。

我收到的消息是参数用户名丢失。

提前致谢。

最佳答案

我发现了这个问题,我没有编写网络服务,显然以 JSON 格式发送数据是错误的。 如果我以“¶m=value¶m2=value2”格式发布数据,它就可以工作。

    NSString *JSONString = [NSString stringWithFormat:@"&username=%@&password=%@",
                      [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                      [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *JSONBody = [JSONString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = @"POST";
loginRequest.HTTPBody = JSONBody;

关于objective-c - iOS JSON 身份验证 token 请求显示缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695280/

相关文章:

javascript - 将错误的 JSON 字符串转换为 JSON 数组

objective-c - cocoa 绘图入门

ios - 主线程上昂贵的 renderInContext 内存使用

ios - URLPathAllowedCharacterSet 不编码感叹号

iphone - 我可以在 iPhone 上创建后台服务吗?

ios - Metal iOS 中的 tex2Dproj 等价物

iphone - ASIFormDataRequest "_OBJC_CLASS_$_ASIFormDataRequest"中的错误,引用自 :

objective-c - 在不分配该类的情况下访问另一个类中的属性?

json - Mongodb导入错误

javascript - php 中的 MySql 查询仅在以 json 编码时返回最后的结果