我从登录api响应中获得一个令牌号码,我需要在http头域中的URL中发送此令牌。请先查看我从登录api获得的回复。
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImpvaG4uc21pdGhAZ21haWwuY29tIiwiaWQiOiI1NzFkYzI3NmU0YjA1NjVmNTcwZjM2ZGQiLCJpYXQiOjE0NjMwMzY2Nzd9.3p2lXjOvQ-iIJZWr4GwuHCYf9VCDZbb3l9O1a8d7Eqs","data":{"name":"John Smith","role":"driver"},"message":"success"}
现在我需要在另一个url中发送这个toke并将其保存在字符串中。请查看标题字段的代码。
- (void)viewDidLoad {
[super viewDidLoad];
token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Accesstoken"];
NSLog(@"Accesstoken %@", token);
NSURL *theURL = [NSURL URLWithString:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
//Specify method of request(Get or Post)
[theRequest setHTTPMethod:@"GET"];
//Pass some default parameter(like content-type etc.)
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
//Now pass your own parameter
[theRequest setValue:token forHTTPHeaderField:@"Authorization"];
NSLog(@"req %@", theRequest);
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
//Now you can create a NSDictionary with NSJSONSerialization
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
NSLog(@"url to send request= %@",theURL);
NSLog(@"%@",dataDictionaryResponse);
}
答案 0 :(得分:0)
试试这个 - >>
NSString *stringURL=[NSString stringWithFormat:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
[request addValue:@"token" forHTTPHeaderField:@"Authorization"];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:returnData
options:kNilOptions error:nil];
NSLog(@"--->%@",son);
后>
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL * url = [NSURL URLWithString:[ NSString stringWithFormat:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"]];;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"token" forHTTPHeaderField:@"Authorization"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *mapData = [[NSMutableDictionary alloc] init];//
[mapData setValue:@"Anyname" forKey:@"useremail"];
[mapData setValue:@"Anypwd" forKey:@"password"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data>>>> = %@",text);
}
}];
[postDataTask resume];