R/http.R

Defines functions apple_captive_test http_get

Documented in apple_captive_test

http_get <- function(url) {
  tmp <- tempfile()
  on.exit(unlink(tmp, recursive = TRUE), add = TRUE)

  suppressWarnings(utils::download.file(url, tmp, quiet = TRUE))

  if (!file.exists(tmp)) stop("Cannot download `", url, "`")

  readChar(tmp, file.info(tmp)$size, useBytes = TRUE)
}

#' Download Apple's captive portal test
#'
#' If the test page, returns "Success" that means that the computer is
#' connected to the Internet.
#'
#' Note that this function will fail if the computer is offline. Use
#' [is_online()] to check if the computer is online.
#'
#' @export
#' @examplesIf pingr:::safe_examples()
#' apple_captive_test()

apple_captive_test <- function() {
  out <- http_get("http://captive.apple.com/hotspot-detect.html")
  grepl("Success", out)
}

Try the pingr package in your browser

Any scripts or data that you put into this service are public.

pingr documentation built on Oct. 27, 2022, 1:06 a.m.