自定义NSURLProtocol拦截到的HTTPBody为空
2018-03-14 本文已影响3人
ioser_sk
由于升级AFNetWorking新版本使用了NSURLSession,老版本使用的是NSURLConnection,项目中所有POST请求拦截到的HTTPBody为空。具体解决方案如下:
if ([req.HTTPMethod isEqualToString:@"POST"]) {
if (!req.HTTPBody) {
uint8_t rd[1024] = {0};
NSInputStream *stream = request.HTTPBodyStream;
NSMutableData *data = [[NSMutableData alloc] init];
[stream open];
while ([stream hasBytesAvailable]) {
NSInteger len = [stream read:rd maxLength:1024];
if (len > 0 && stream.streamError == nil) {
[data appendBytes:(void *)rd length:len];
}
}
req.HTTPBody = [data copy];
[stream close];
}
}