API 호출을 사용하는 앱 iPhone 앱이 있습니다. 나는 데이터 수신을 위해 NSURLSession이 서버로 토큰을 다시 보냅니다.
NSString *username = @"admin";
NSString *password = @"test";
NSString *authString = [NSString stringWithFormat:@"%@:%@",
username,
password];
// 2 - convert authString to an NSData instance
NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
// 3 - build the header string with base64 encoded data
NSString *authHeader = [NSString stringWithFormat: @"Basic %@",
[authData base64EncodedStringWithOptions:0]];
// 4 - create an NSURLSessionConfiguration instance
NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
// 5 - add custom headers, including the Authorization header
[sessionConfig setHTTPAdditionalHeaders:@{
@"Accept": @"application/json",
@"Authorization": authHeader
}
];
// 6 - create an NSURLSession instance
NSURLSession *session =
[NSURLSession sessionWithConfiguration:sessionConfig delegate:self
delegateQueue:nil];
// 7 - create an NSURLSessionDataTask instance
NSString *urlString = @"http://test.myserver.am/api/authentication/Login?username=admin&password=test";
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionDataTask *task = [session dataTaskWithURL:url
completionHandler:
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
if (error)
{
// do something with the error
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
arrTokenData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self getDomain];
} else {
// failure: do something else on failure
NSLog(@"httpResponse code: %@", [NSString stringWithFormat:@"%ld", (unsigned long)httpResponse.statusCode]);
NSLog(@"httpResponse head: %@", httpResponse.allHeaderFields);
return;
}
}];
// 8 - resume the task
[task resume];
가 지금은 ......
사용자 데이터를 얻기 위해 다른 통화를 서버로부터 수신 한 토큰을 사용하여 만드는 중이라서 ....
NSURLSession
및 서버에서 토큰을 수신하여 사용자 이름/암호를 사용하여 성공적으로 전화를했다
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *authValue = [arrTokenData valueForKey:@"Token"];
//Configure session with common header fields
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{@"bearer": authValue};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSString *url = @"http://test.myserver.am/api/mobile/LookUps/getuserdata";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
//Process the data
}
}
}];
[task resume];
하지만 난 상태 코드 아래 수신하고 있고 .... 요청이 성공 못하고있다 ....
HttpResponse에 코드 : 500 HttpResponse에 머리 : { "캐시 제어"= "N o-cache "; "Content-Length"= 36; "Content-Type"= "application/json; charset = utf-8"; 날짜 = "목, 2016 년 10 월 6 일 12:16:58 GMT"; Expires = "-1"; Pragma = "no-cache"; 서버 = "Microsoft-IIS/8.5"; "X-AspNet-Version"= "4.0.30319"; "X-Powered-By"= "ASP.NET"; }
**** 잘 작동 동일한 API를 유의하시기 바랍니다 내가 목표 - C를 사용하고 다른 (APP 자 마린) 플랫폼 ... **
....에서 IOS10
내 보내는 토큰 요청이 적절하지 않은가 ....?
날 좀 도와주세요 ..... 내가 먼저 ... 어제 여기
안녕하세요, 답장을 보내 주셔서 대단히 감사합니다. 일단 모든 기본적인 조건을 가진 내 코드를 업데이 트됩니다 기본 통신 서버와 적절한. 또한 진행 상황이 어떻게 내가 상태 코드 200 또한 오류가 발생하지 않습니다하지만 난 그물 데이터에 액세스 할 수 .... NSDictionary * jsonData = [NSJSONSerialization JSONObjectWithData : 데이터 옵션 : NSJSONReadingMutableContainers | NSJSONReadingAllowFragments 오류 : 없음]; jsonData는 0 레코드를 보여줍니다. – user2813740
쉽게 인증 부족이 될 수 있습니다. 인증 헤더가 실제로 전송되는지 확인하기 위해 Charles Proxy를 실행 해 보셨습니까? 이 헤더는 일반적으로 OS에서 사용하기 위해 예약되어 있으므로 밟기 시작할 수 있습니다. 개별 요청에 명시 적으로 지정해야 할 수도 있습니다. 그렇더라도 전체적으로 먹을 수도 있습니다.이 경우 대신 X-Authorization으로 보내보십시오. – dgatwood
안녕하세요, 정말 고마워요. 토큰을 보내고 데이터를 다시 가져 오는 것과 통신 할 수 있습니다. API에 관한 문서가 .net 팀에서 ETC라는 매개 변수를 받았습니다. 일부 시간 데이터가 사전, 배열 또는 내부 jsonData => 데이터로 제공되므로 혼란이있었습니다. 다시 한번 감사드립니다. – user2813740