简体   繁体   中英

Why iOS 8.0 simulator in Xcode 6.0.1 could not fetch cookie correctly

After upgrade to Xcode 6, When I fetch cookies from a login "POST" action, like this:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/login.php", kDSZHDomainString]];
ASIFormDataRequest *asirequest = [ASIFormDataRequest requestWithURL:url];
[asirequest setPostValue:username forKey:@"u"];
[asirequest setPostValue:password forKey:@"p"];
[asirequest setTag:kLoginRequest];
[asirequest setStringEncoding:-2147481083];
[asirequest setTimeOutSeconds:600];
[asirequest setDelegate:self];
[asirequest startAsynchronous]; 

when I set breakpoint to get request's headers:

NSMutableDictionary * headers = [request requestHeaders];

debug info:

Printing description of headers:
{
    "Accept-Encoding" = gzip;
    "Content-Length" = 20;
    "Content-Type" = "application/x-www-form-urlencoded; charset=hz-gb-2312";
    Cookie = "__utma=213790228.284667840.1411460847.1411460847.1411460847.1; __utmz=213790228.1411460847.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); o=m; s=542366a13a44e; sv[1]=12532852%2C12532147%2C12534523; v[11]=12515374; v[1]=12534523; v[2]=12521992; v[3]=12527199; v[9]=12441637";
    "User-Agent" = "Top81 0.7 rv:0.7.459 (iPhone Simulator; iPhone OS 8.0; en_US)";
}

In iOS 7 simulator, headers are:

Printing description of headers:
{
    "Accept-Encoding" = gzip;
    Cookie = "o=m; s=542369d4ede78; sv[1]=12532886%2C12528442%2C12534551; u=xxxx%2Ca999b1c0d3663e236f58277302b8be90%2C0; v[1]=12534551";
    "User-Agent" = "Top81 0.7 rv:0.7.459 (iPhone Simulator; iPhone OS 7.1; en_US)";
}

so, cookie "u" fetched correctly.

I test the code with ASIHTTPRequest and AFNetworking, and get same result.

Is it the Xcode and iOS 8 simulator's bug or any code to resolve this problem?

I have used afnetworking and it is working fine in ios 8. Here's the function

+(void)postData : (NSString *)url parameters:(NSDictionary *)paraDict  success: (void (^) (NSDictionary *responseStr))success failure: (void (^) (NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];

    manager.responseSerializer =  [AFHTTPResponseSerializer serializer];

    AFHTTPRequestOperation *op = [manager POST:url parameters:paraDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    } success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSError* error = nil;


        success([NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error]);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        failure(error);
    }];
    [op start];

}

Here url is string not nsurl which should be like @" http://www.google.com "

and paradict is dictionary.U can make it like

NsDictionary *paraDict = @{@"username":@"u",@"password":@"p"}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM