0

我正在与执行 304 重定向的 REST API 作斗争;我需要的是获取重定向的目标 URL 并用浏览器打开它(我知道,这有点变态)。我成功拦截了重定向,这要归功于这个可爱的小伙子 reversepanda: https ://github.com/bustoutsolutions/siesta/issues/210 但我仍然不知道如何在 GET 请求的回调中获取重定向 URL(成功或失败)

resource.load().onSuccess{ response in
        //HERE I WOULD LIKE TO TAKE THE URL OF THE REDIRECT 
        //(if I print the response I can see the HTML of the destination web page where the user should land)
    }.onFailure{ error in
        //using 'completionHandler(nil)' in the 'willPerformHTTPRedirection' method of the delegate, brings me here
    }

关于如何解决这个问题的任何建议?

谢谢!

4

1 回答 1

0

看看 RequestChain.swift 里面,它有一些可以提供帮助的示例注释。我相信您可以执行以下操作:

func redirectRequest() -> Request {
  return self.yourAnotherRequest(onSuccess: {
    }, onFailure: { error in
  })
}

func yourRequest(request: Siesta.Request) -> Request {
  return request.chained {
    guard case .failure(let error) = $0.response,
        error.httpStatusCode == 401 else {
            return .useThisResponse
    }

    return .passTo(
        self.redirectRequest().chained {
            if case .failure = $0.response {
                return .useThisResponse
            } else {
                return .passTo(request.repeated())
            }
        }
    )
  }
}

您可以使用关键字搜索更多示例chaineduseThisResponsepassTo在 Siesta 资源中搜索。

请让我们知道它是否有助于解决您的问题,很高兴看到您的最终解决方案。

于 2018-07-11T11:44:44.810 回答