在 AFNetworking 中,httpbody 和参数 post 请求之间的区别?
In AFNetworking, difference between httpbody and parameter post request?
首先使用 httpbody:
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"url" parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:da];
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSLog(@"Reply JSON: %@", responseObject);
} else {
NSLog(@"Error: %@, %@, %@", error, response, responseObject);
}
}] resume];
第二个带参数:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager POST:@"https://exmaple.com/post.php" parameters:json progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
这两种post方法有什么不同?我注意到一个不同的是参数一个将使用 url 编码方法对数据进行编码,而另一个将传递原始数据。
第一种方法适用于将单个原始数据块传递到服务器的情况。用于:
- 将 JSON 数据的 blob 发送到需要 JSON 数据的 CGI
- 以URL编码或表单数据编码格式发送预编码正文数据
- 发送到支持 WebDAV 的服务器的 PUT 请求中的文件数据。
第二种方法(提供一系列参数)旨在通过将正文数据提供为一系列 URL 编码的键值对来模拟表单提交。对于大多数非 JSON 基础的 CGI 作品,这就是您想要的。
使用哪一个的决定在很大程度上取决于服务器端发生的情况。如果脚本期望正文数据是 JSON 的 blob,请将 JSON 数据编码为 NSData 对象并将其作为正文数据发送。如果脚本需要 HTML 形式的结果,请使用其他方法。如果脚本不关心,则使用平均发送较少数据的方法。 :-)
首先使用 httpbody:
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"url" parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:da];
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSLog(@"Reply JSON: %@", responseObject);
} else {
NSLog(@"Error: %@, %@, %@", error, response, responseObject);
}
}] resume];
第二个带参数:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager POST:@"https://exmaple.com/post.php" parameters:json progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
这两种post方法有什么不同?我注意到一个不同的是参数一个将使用 url 编码方法对数据进行编码,而另一个将传递原始数据。
第一种方法适用于将单个原始数据块传递到服务器的情况。用于:
- 将 JSON 数据的 blob 发送到需要 JSON 数据的 CGI
- 以URL编码或表单数据编码格式发送预编码正文数据
- 发送到支持 WebDAV 的服务器的 PUT 请求中的文件数据。
第二种方法(提供一系列参数)旨在通过将正文数据提供为一系列 URL 编码的键值对来模拟表单提交。对于大多数非 JSON 基础的 CGI 作品,这就是您想要的。
使用哪一个的决定在很大程度上取决于服务器端发生的情况。如果脚本期望正文数据是 JSON 的 blob,请将 JSON 数据编码为 NSData 对象并将其作为正文数据发送。如果脚本需要 HTML 形式的结果,请使用其他方法。如果脚本不关心,则使用平均发送较少数据的方法。 :-)