gpt4 book ai didi

ios - NSURLSession 将 token 发送回服务器以获取数据

转载 作者:行者123 更新时间:2023-11-29 00:39:12 26 4
gpt4 key购买 nike

我有一个 iPhone 应用程序,它将使用 API 的调用。我使用 NSURLSession 成功调用了用户名/密码,并从服务器接收了 token ....

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];

现在我正在使用从服务器收到的 token 并再次调用以获取用户数据......

[[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];

但是我收到以下状态代码并且......请求未成功......

http响应代码:500httpResponse head: { “Cache-Control” = “no-cache”; “内容长度”= 36; "Content-Type"= "application/json; charset=utf-8";日期 =“2016 年 10 月 6 日星期四 12:16:58 GMT”;过期 = "-1"; Pragma = "无缓存";服务器=“微软-IIS/8.5”; "X-AspNet-版本"= "4.0.30319"; “X-Powered-By”=“ASP.NET”;

****请注意,相同的 API 在另一个(xamarin APP)平台上运行良好......**

我正在使用 Objective-C...IOS10

我的发送 token 请求是否不正确......?

请帮帮我......我从昨天就被困在这里......

最佳答案

正如已经提到的,我很确定它应该是:

    NSString *authValue = [NSString stringWithFormat:@"Bearer %@",
[arrTokenData valueForKey:@"Token"]];
sessionConfiguration.HTTPAdditionalHeaders = @{@"Authorization": authValue};

话虽如此,500 错误是内部服务器错误,而不是身份验证错误。看起来真正的问题可能与身份验证无关,而且您的请求本身在某种程度上存在格式错误,或者服务器端代码中存在您不知何故发痒的错误。

此外,您的代码似乎没有检查响应中是否实际存在 token ,除非您在其他地方这样做。

我会首先检查以确保 token 确实存在,如果存在,请启用您可以在服务器上启用的任何调试并查看日志以尝试找出导致服务器上出现 500 错误的原因边。很有可能,一旦您看到服务器端实际发生了什么,修复就会很明显。

关于ios - NSURLSession 将 token 发送回服务器以获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com