AFNetworking — get redirect URL

馋奶兔 提交于 2019-12-05 19:27:59
-(void)call
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:YOUR_PARAMETER forKey:KEY];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:YOUR_WEBSERVICE_URL];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:WEBSERVICE_NAME parameters:params];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //This is Redirect URL 
         NSLog(@"%@",[[[operation response] URL] absoluteString]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Failure");
     }];

     [operation start];
}

I know this is a bit old, but I ran into this same type of problem. What helped me achieve finding the redirect URL was the following code snippet. I was able to get my redirect url out of the jsonDict.

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id jsonObject){
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"json: %@", jsonDict);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
    NSLog(@"failure");
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!