我查遍了一切,似乎无法弄清楚这一点。 我看到的 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/