我有應用程序將使用API調用的iPhone應用程序。 我做了用戶名/密碼成功調用與NSURLSession
,並接收來自服務器令牌....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];
我現在用的令牌從服務器接收和撥打另一個電話獲取用戶數據......
[[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鄰高速緩存「; 「Content-Length」= 36; 「Content-Type」=「application/json; charset = utf-8」;日期=「星期四,2016年10月06日12:16:58 GMT」; Expires =「-1」; Pragma =「no-cache」; Server =「Microsoft-IIS/8.5」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; }
****請注意相同的API工作正常,從另一個(xamarin APP)平臺.... **
我使用的Objective-C .... IOS10
有我的發送令牌請求是不正確的....?
請幫我.....我首先堅持在這裏從昨天...
你好,非常感謝你的回覆。一旦我獲得與服務器的基本通信,我將用所有標準更新我的代碼。也有進步,我怎麼也沒有得到任何錯誤也是狀態碼200,但我可以淨訪問數據.... NSDictionary * jsonData = [NSJSONSerialization JSONObjectWithData:數據選項:NSJSONReadingMutableContainers | NSJSONReadingAllowFragments錯誤:無]; jsonData顯示0條記錄。 – user2813740
這可能很容易缺乏身份驗證。您是否嘗試過運行Charles Proxy來查看驗證頭是否實際發送?這個頭文件通常保留給操作系統使用,所以它可能會被禁止。您可能必須在單個請求中明確指定它,即使您這樣做了,也可能會完全被吃掉,在這種情況下,請嘗試將它作爲X-Authorization發送。 – dgatwood
您好,非常感謝您..我可以與GET通信發送令牌並獲取數據..從.net團隊獲取有關API調用參數ETC的文檔。有些時候數據以字典,數組或內部jsonData =>數據出現時會出現混淆。再一次感謝你。 – user2813740