【问题标题】:Why does NSURLConnection stop sending data if you return nil from connection:willSendRequest:redirectResponse:如果从 connection:willSendRequest:redirectResponse 返回 nil,为什么 NSURLConnection 会停止发送数据:
【发布时间】:2013-10-19 12:45:12
【问题描述】:

我正在为 NSURLConnection 实现一个重定向处理程序。出于我的目的,我实际上并不想进行重定向,我想加载整个页面并记住重定向以供以后使用。在 NSURLConnectionDataDelegate 的 connection:willSendRequest:redirectResponse: 方法的文档中,我发现:

根据重定向响应使用的实际 URL 请求。 委托可以返回未经修改的请求以允许重定向, 返回一个新的请求,或者返回 nil 来拒绝重定向和 继续处理连接。

我试过这段代码:

- (NSURLRequest *) connection: (NSURLConnection *) connection
              willSendRequest: (NSURLRequest *) request
             redirectResponse: (NSURLResponse *) redirectResponse
{
    NSURL *url1 = [request URL];
    NSURL *url2 = [redirectResponse URL];
    if ([request URL] != nil)
        self.redirect = [request URL];
    return nil;
}

- (void) connection: (NSURLConnection *) theConnection didReceiveResponse: (NSURLResponse *) response {
    NSURL *url1 = [response URL];
    int foo = 0;
}

该程序从未调用过connection:didReceiveData:connection:didFailWithError:connectionDidFinishLoading:。返回request 而不是nil,程序正常加载页面。 (重定向的 URL 是原始 URL,但那是 another question。如果它们相关,请杀死两只鸟并回答那个。)

在重定向尝试后正确允许 NSURLConnection 继续处理文件是否需要其他东西?

【问题讨论】:

    标签: ios nsurlconnection


    【解决方案1】:

    HTTP 重定向响应在大多数情况下是当前连接的结束。如果您的第一个连接以重定向结束,而您拒绝处理重定向,则无需再做任何事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2014-11-11
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多