POST 请求 iOS:幕后发生了什么?
POST request iOS: what's happening under the hood?
我遇到了一个非常奇怪的问题,我希望有人能帮助我了解三天前我遇到的问题在哪里。
简单地说,我将 NSMutableRequest 发送到一个简单的端点,我在其中发送(使用 POST)一个 json 并且我应该收到响应 0 或 1。
该应用程序第一次在设备上 运行 时代码有效,但不知何故在接下来的几次中它不再有效。
为了更好地解释自己,如果我每次都卸载并重新安装该应用程序,我会得到正确的响应,但是如果我第二次 运行 代码两次,我会得到类似 [CSRF 验证失败] 的信息端点。这个错误意味着我没有发送正确的格式(或者我发送了一些奇怪的东西)。
我的问题是:这怎么可能?有没有可能我要发送其他东西?
端点工作正常,因为使用 android 版本我没有任何问题...
代码如下,希望有人能解释一下幕后发生的事情以及我如何设法解决这个问题。
NSString *mail = [profile valueForKey:@"email"];
NSString *provider = [profile valueForKey:@"provider"];
// making a GET request to endpoint
NSString *baseUrl = ENDPOINT_URL;
NSString *targetUrl = [NSString stringWithFormat:@"%@", baseUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSString *body = [NSString stringWithFormat:@"{\"mail\":\"%@\", \"provider\":\"%@\"}",mail,provider];
NSData *postData=[body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postData];
[request setURL:[NSURL URLWithString:targetUrl]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
if (data){
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", myString);
if ([myString isEqualToString:@"[\"CSRF validation failed\"]"]){
NSLog(@"ENDPOINT ERROR");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
} else {
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Data received: %@", json);
NSNumber *value = [NSNumber numberWithInt:[[json objectForKey:@"profile_exists"] intValue]];
if ([value intValue] == 1){
//Profile exists.
NSLog(@"Profile exists.");
[self silentLogin:profile];
} else if ([value intValue] == 0) {
//Profile does not exists.
NSLog(@"Profile does not exist.");
[self silentRegistration:profile];
}
else {
//Error.
NSLog(@"Error in ENDPOINT VALUE");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
}
NSLog(@"%@",json);
}
} else {
NSLog(@"No Data received");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
}
}] resume];
我解决了每次请求都删除 cookie 的问题。好像每次都重新发送这些cookies。
+ (void)clearCookiesForURL: (NSString *)url {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:url]];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
[cookieStorage deleteCookie:cookie];
}
}
我遇到了一个非常奇怪的问题,我希望有人能帮助我了解三天前我遇到的问题在哪里。
简单地说,我将 NSMutableRequest 发送到一个简单的端点,我在其中发送(使用 POST)一个 json 并且我应该收到响应 0 或 1。
该应用程序第一次在设备上 运行 时代码有效,但不知何故在接下来的几次中它不再有效。
为了更好地解释自己,如果我每次都卸载并重新安装该应用程序,我会得到正确的响应,但是如果我第二次 运行 代码两次,我会得到类似 [CSRF 验证失败] 的信息端点。这个错误意味着我没有发送正确的格式(或者我发送了一些奇怪的东西)。
我的问题是:这怎么可能?有没有可能我要发送其他东西?
端点工作正常,因为使用 android 版本我没有任何问题...
代码如下,希望有人能解释一下幕后发生的事情以及我如何设法解决这个问题。
NSString *mail = [profile valueForKey:@"email"];
NSString *provider = [profile valueForKey:@"provider"];
// making a GET request to endpoint
NSString *baseUrl = ENDPOINT_URL;
NSString *targetUrl = [NSString stringWithFormat:@"%@", baseUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSString *body = [NSString stringWithFormat:@"{\"mail\":\"%@\", \"provider\":\"%@\"}",mail,provider];
NSData *postData=[body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postData];
[request setURL:[NSURL URLWithString:targetUrl]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
if (data){
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", myString);
if ([myString isEqualToString:@"[\"CSRF validation failed\"]"]){
NSLog(@"ENDPOINT ERROR");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
} else {
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Data received: %@", json);
NSNumber *value = [NSNumber numberWithInt:[[json objectForKey:@"profile_exists"] intValue]];
if ([value intValue] == 1){
//Profile exists.
NSLog(@"Profile exists.");
[self silentLogin:profile];
} else if ([value intValue] == 0) {
//Profile does not exists.
NSLog(@"Profile does not exist.");
[self silentRegistration:profile];
}
else {
//Error.
NSLog(@"Error in ENDPOINT VALUE");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
}
NSLog(@"%@",json);
}
} else {
NSLog(@"No Data received");
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication] delegate] loginAborted];});
}
}] resume];
我解决了每次请求都删除 cookie 的问题。好像每次都重新发送这些cookies。
+ (void)clearCookiesForURL: (NSString *)url {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:url]];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
[cookieStorage deleteCookie:cookie];
}
}