从 NSURLConnection 捕获重定向地址
我正在开发一个遵循 HTTP 重定向的软件,该重定向由服务器根据参数动态计算。我不想在 Mobile Safari 中显示主服务器,而只想显示重定向的地址。 以下代码有效:
request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Extract the redirected URL
target = [response URL];
问题是服务器需要几秒钟才能应答。 sendSynchronousRequest
这次完全阻止了应用程序,这很混乱,我什至无法显示“Busy”动画。
有谁知道如何在不出现 Safari 的情况下异步检索重定向地址以及重定向服务器 URL,或者在 sendSynchronousRequest
期间显示某种“耐心等待”动画?在另一个线程中传递 sendSynchronousRequest
会有什么缺点?
I'm working on a software which follows the HTTP redirection which is dynamically calculated by the server depending on a pparameter. I don't want to show the primary server in Mobile Safari but rather the redirected address only.
The following code workks:
request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Extract the redirected URL
target = [response URL];
The problem is that the server requires several seconds to answer. The sendSynchronousRequest
blocks the app for this time completely which is messy, I can't even display the "Busy" animation.
Does anyone know how I can retrieve the redirected address asynchronously without safari appearance in the meanwhile with the redirecting server URL or display some sort of the "Be patient" animation during the sendSynchronousRequest
? What disadvantages would have the passing of sendSynchronousRequest
in another thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NSURLConnection 提供的异步行为并提供与实现 -connection:willSendRequest:redirectResponse:
然后阅读 同步网络不好的五个原因
Use the asynchronous behavior provided by
NSURLConnection
and provide the connection with a delegate which implements -connection:willSendRequest:redirectResponse:Then go read Five Reasons Why Synchronous Networking Is Bad