CFNetwork HTTP timeout?

2019-08-24 14:34发布

I am looking for a way to add a timeout to a CFHTTP request. It seems like there should be a feature of the CFHTTPMessage or the CFReadStream object, but I can't find it. Do I have to roll my own timer on the run loop or something? (if so, any code for this?)

Thanks!

2条回答
在下西门庆
2楼-- · 2019-08-24 14:58

Try this, readStream is your CFReadStreamRef:

#define _kCFStreamPropertyReadTimeout CFSTR("_kCFStreamPropertyReadTimeout")

double to = 15; // 15s timeout
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &to);
CFReadStreamSetProperty(readStream, _kCFStreamPropertyReadTimeout, num);
CFRelease(num);

Constant for write timeout is _kCFStreamPropertyWriteTimeout

查看更多
Luminary・发光体
3楼-- · 2019-08-24 15:14

My answer is outdated but I can't delete the accepted answer.

查看更多
登录 后发表回答