在开发中, 我们经常需要在网络请求时修改HTTP/HTTPS的请求头信息
1.普通AFN请求
#import "LMHTTPSessionManager.h" #import <AFNetworking/AFNetworking.h> @interface LMHTTPSessionManager : AFHTTPSessionManager @end @implementation LMHTTPSessionManager - (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration
{
if (self = [super initWithBaseURL:url sessionConfiguration:configuration]) {
[self.requestSerializer setValue:Version forHTTPHeaderField:@"1.0"];
[self.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"SYSTYPE"];
[self.requestSerializer setValue:@"XXX/iOS/v"forHTTPHeaderField:@"User-Agent"];
}
return self;
}
@end
2.AVPlayer播放视频链接修改请求头
由于公司做视频原创网站, 盗链现象特别严重, 每个月公司都要损失很多钱, 老板就火了, 做防盗链, 请求头认证客户端, 然后我就一脸懵逼啊, 国内百度, 国外谷歌啊, AVFoundation中的几个类头文件都翻遍了, 是在实找不到怎么修改请求头信息啊
后面虽然解决了, 还是写篇文章, 方便下后面的开发者吧
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
[headers setObject:@"YourHeader"forKey:@"User-Agent"];
self.urlAsset = [AVURLAsset URLAssetWithURL:self.videoURL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
// 初始化playerItem
self.playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
from:https://www.jianshu.com/p/31de0ef8ae97