简体   繁体   English

为什么Xcode 6.0.1中的iOS 8.0模拟器无法正确获取Cookie

[英]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: 升级到Xcode 6之后,当我通过登录“ POST”操作获取Cookie时,如下所示:

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: 在iOS 7模拟器中,标头为:

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. 因此,cookie“ u”正确获取。

I test the code with ASIHTTPRequest and AFNetworking, and get same result. 我使用ASIHTTPRequest和AFNetworking测试代码,并得到相同的结果。

Is it the Xcode and iOS 8 simulator's bug or any code to resolve this problem? 是Xcode和iOS 8模拟器的bug还是解决此问题的任何代码?

I have used afnetworking and it is working fine in ios 8. Here's the function 我已经使用过afnetworking,并且在ios 8中工作正常。这是功能

+(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 " 此处的url是不是nsurl的字符串,应类似于@“ http://www.google.com

and paradict is dictionary.U can make it like 参数是字典。U可以使它像

NsDictionary *paraDict = @{@"username":@"u",@"password":@"p"} NsDictionary * paraDict = @ {@“用户名”:@“ u”,@“密码”:@“ p”}

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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