0
我是初學者,處理iOS中的連接。我想對一個url執行一個POST請求和一個GET請求。問題是我需要首先執行POST,這樣我才能獲得以JSON解析的訪問標記,稍後將放置在GET請求的標題中,以便檢索我需要的登錄數據。我正在使用自定義委託方法,但是當我運行此操作時,GET請求比POST執行得早,所以在GET完成之前我無法獲得密鑰。有沒有辦法先執行POST,然後執行GET?這裏是我的代碼:執行HTTP POST請求,然後執行HTTP GET請求
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//params
self.user = @"carlos";
self.pass = @"1234";
self.grantType = @"password";
self.clientId = @"7da713e69afc96cf894e";
self.clientSecret = @"2c14ec54cfdfd6faec4ef56ca7f0870ab83f820b";
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
self.sideBarButton.target = self.revealViewController;
self.sideBarButton.action = @selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self httpPostWithCustomDelegate];
[self sendHTTPGet];
}
-(void) httpPostWithCustomDelegate
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://192.237.241.175:8090/oauth2/access_token/"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =[NSString stringWithFormat:@"&username=%@&password=%@&grant_type=%@&client_id=%@&client_secret=%@&scope=write",self.user,self.pass,self.grantType,self.clientId,self.clientSecret];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Retrieving Data");
// NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
//Test Display
// NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// NSLog(@"Data = %@",text);
self.responseDataPost = [[NSMutableData alloc] init];
[self.responseDataPost appendData:data];
NSError *error;
NSMutableDictionary *receivedData = [NSJSONSerialization JSONObjectWithData:self.responseDataPost options:NSJSONReadingMutableContainers error:&error];
//obtener del dictionary el access token y meter como header Authorization Bearer + id
//NSArray *accessToken = [receivedData valueForKeyPath:@"access_token"];
self.connTxtPost.text = [receivedData valueForKeyPath:@"access_token"];
self.recData = [receivedData valueForKeyPath:@"access_token"];
}
}];
[dataTask resume];
}
-(void) sendHTTPGet
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://192.237.241.175:8090/snippets/"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"GET"];
self.connTxtGet.text = self.recData ;
[urlRequest setValue:self.recData forHTTPHeaderField:@"Authorization"];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
}
優秀!!!工作得很好!!!非常感謝你!!! (: –
不客氣.. :) – Rashad