Objective c 我正在用带有头的摘要身份验证实现api,而不是从下面的代码获得响应
最后,我找到了使用头的摘要身份验证解决方案 -(无效)洛吉纳皮 {Objective c 我正在用带有头的摘要身份验证实现api,而不是从下面的代码获得响应,objective-c,Objective C,最后,我找到了使用头的摘要身份验证解决方案 -(无效)洛吉纳皮 { }“无响应”,意思是什么?是否调用了completionHandler?dicnil吗?(您没有将NSError参数放入NSJSONSerialization)。数据是否为零?到底发生了什么?我运行您的代码,它显示如下响应数据={“状态”:false,“错误”:“无效凭据”}如果您使用表单数据(empcode:divya)点击postman中的api。p@ecreeds.com,密码:cc03e747a6afbbcbf8be76
}“无响应”,意思是什么?是否调用了
completionHandler
?dic
nil吗?(您没有将NSError
参数放入NSJSONSerialization
)。数据是否为零?到底发生了什么?我运行您的代码,它显示如下响应数据={“状态”:false,“错误”:“无效凭据”}如果您使用表单数据(empcode:divya)点击postman中的api。p@ecreeds.com,密码:cc03e747a6afbbcbf8be7668acfebee5)和摘要身份验证标头,u将获取用户登录详细信息。但从代码中,它将显示无效凭据。
-(void)httpPostWithCustomDelegate
{
NSString *st = @"Digest username=\"admin\", realm=\"REST API\", nonce=\"1\", uri=\"/canteen/restserver/index.php/welcome/login\", qop=auth, nc=2, cnonce=\"2\", response=\"0097667c159d30166fb195909955ebac\", opaque=\"1\"";
NSDictionary *headers = @{ @"authorization":st,@"cache-control":@"no-cache"};
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://canteen.mostlovedcountry.com/restserver/index.php/welcome/login"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"empcode=divya.p@ecreeds.com&password=cc03e747a6afbbcbf8be7668acfebee5";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setAllHTTPHeaderFields:headers];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@", dic);
NSLog(@"Response:%@",response);
NSLog(@"%@",error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
}
NSDictionary *headers = @{@"content-type": @"application/x-www-form-urlencoded", @"authorization": @"Digest username=\"admin\", realm=\"REST API\", nonce=\"1\", uri=\"/restserver/index.php/welcome/login\", qop=auth, nc=1, cnonce=\"1\", response=\"e35d1ebfcfa17a119ee0b45c6703cb8e\", opaque=\"1\"", @"cache-control": @"no-cache" };
NSMutableData *postData = [[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"empcode=divya.p@ecreeds.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"&password=cc03e747a6afbbcbf8be7668acfebee5"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://canteen.mostlovedcountry.com/restserver/index.php/welcome/login"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@", dic);
}
}];
[dataTask resume];