2016-03-04 119 views
0
NSString *Str = @"Basic YnRwaXhlbDoxMjM0NQ=="; 
NSArray *keys = [NSArray arrayWithObjects: @"Authorization",nil]; 
NSArray *objects = [NSArray arrayWithObjects:Str,nil]; 
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
NSError * error = nil; 
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONReadingMutableContainers error:&error]; 
NSURL *urlStr1 = [NSURL URLWithString:@"http://192.168.0.49/bubbletop/index.php?route=feed/rest_api/gettoken&grant_type=client_credentials"]; 

NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:urlStr1]; 
[request1 setHTTPMethod:@"POST"]; 
[request1 setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request1 setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request1 setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
[request1 setHTTPBody: jsonData]; 
_connection = [[NSURLConnection alloc]initWithRequest:request1 delegate:self startImmediately:YES]; 


NSData *data1 = [NSData dataWithContentsOfURL:urlStr1]; 


NSDictionary *fashionJson = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:nil]; 
NSLog(@"requestReply: %@", fashionJson); 

在這裏,我傳遞的報頭字段參數作爲Basic YnRwaXhlbDoxMjM0NQ==的關鍵Authorization,但得到的數據作爲零和獲取日誌作爲傳遞參數給JSON郵政法無

requestReply:(空)

回答

0

我想你加入頭域錯

不喜歡

NSString *Str = @"Basic YnRwaXhlbDoxMjM0NQ=="; 
NSArray *keys = [NSArray arrayWithObjects: @"Authorization",nil]; 
NSArray *objects = [NSArray arrayWithObjects:Str,nil]; 
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
NSError * error = nil; 
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONReadingMutableContainers error:&error]; 

不喜歡的HTTP令牌認證

[request1 addValue:@"Basic YnRwaXhlbDoxMjM0NQ==" forHTTPHeaderField:@"Authorization"]; 

注意

在這裏

[request1 setHTTPBody: jsonData]; 

樣品申請無需追加身體看this

+0

是啊現在可以工作了..謝謝你.. –

+0

歡迎....... –

0

由於您使用的是NSData *data1 = [NSData dataWithContentsOfURL:urlStr1];方法,因此它不會將您的httpbody從您創建的url請求中取出。執行NSURLConnection委託方法或使用NSURLSession加載數據。由於NSURLConnection API已棄用,因此我會建議NSURLSession