I am creating an nshttprequest with some value in cookie in headers and sending to server but the server page is not recognizing the cookie and is redirecting to login page.I want to confirm if the cookie is sending correctly with the request or not.Below is the code for the ios part.I am logging all the headers of the request and it is showing the cookie information but the page keeps on redirecting.The same equivalent code is working on android however.Please suggest what could be going on.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *str=[NSString stringWithFormat:@"SERVERURL"];
NSLog(@"url : %@",str);
NSArray *arr=[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:[NSString stringWithFormat:@"SERVERURL"]]];
[request setURL:[NSURL URLWithString:str]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Accept"];
if([arr count]>0)
{
NSHTTPCookie *cookie=arr[0];
NSString *cookieStr=[NSString stringWithFormat:@"%@=%@",cookie.name,cookie.value];
NSLog(@"cookieStr = %@",cookieStr);
[request setValue:cookieStr forHTTPHeaderField:@"Cookie"];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:
arr];
[request setAllHTTPHeaderFields:headers];
}
NSData *requestData = [@"" dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSLog (@"cookie %@",request.allHTTPHeaderFields);
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
config.HTTPShouldSetCookies=YES;
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:requestData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
NSLog(@"%@",response);
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responseStr : %@", responseString );
NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",json);
}];
[uploadTask resume];
Aucun commentaire:
Enregistrer un commentaire