iPhone NSData/NSUrl with cookie

∥☆過路亽.° 提交于 2019-11-30 19:25:35

问题


I'm trying to play/stream a mp3 hosted on a website. The site requires a cookie header to be set, but I'm having trouble setting that or getting the container to do that for me.

 NSURL *sampleUrl = [NSURL URLWithString:@"http://domain/files/sample.mp3"];
 NSData *sampleAudio = [NSData dataWithContentsOfURL:sampleUrl];

Up until this point, I've been using jQuery to do/manage XMLHTTPRequests, but I've had to call into native code to stream the audio. It doesn't look like the cookie is getting picked up by the native HTTP request.

Is there anyway to inject the cookie into the above request, or otherwise ensure that a cookie gets added against a given domain?

Thanks


回答1:


Ok, so the way to do this is not injecting headers but setting a cookie manager to always accept cookies. This will then pass on the cookie to subsequent requests.

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

Robbie



来源:https://stackoverflow.com/questions/1005521/iphone-nsdata-nsurl-with-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!