我有R 4.20+,所以我相信utils::download.file正在使用功能libcurl。
我可以用base::curlGetHeaders(url)获取url的头。
是否有一个参数可以在download.file中传递以返回标头,因此不能在同一个调用中获得它们。在遮罩下,download.file正在以某种方式处理报头,因为它正在接收它。
如何从函数curlGetHeaders(url)返回使用download.file获得的响应头
我知道外部包(例如Rcurl),但是要下载,必须在R:::base中接收报头。
更新
这是R的源代码
"libcurl" = {
headers <- if(length(headers)) paste0(nh, ": ", headers)
status <- .Internal(curlDownload(url, destfile, quiet, mode, cacheOK,
headers))
},函数curlDownload在这里有传统的curl选项(libcurl.c):
curl_easy_setopt(hnd[i], CURLOPT_HTTPHEADER, headers);设置标题,而不是返回它。为什么未公开原始curl函数。C和PHP一样公开它们..。看见
Can PHP cURL retrieve response headers AND body in a single request?
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...
$response = curl_exec($ch);所以我想curlDownload需要:
curl_easy_setopt(hnd[i], CURLOPT_HEADER, 1);图书馆(卷曲)
在这个库中,在掩码下面,使用的语法是相同的。如何直接向我公开语法?来自download.c
curl_easy_setopt(handle, CURLOPT_URL, NULL);
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);发布于 2022-11-16 04:14:24
这个文档在curl包中提到了handle_data函数。
https://cran.r-project.org/web/packages/curl/curl.pdf
例如,假设您需要在读取所有行之前查看状态代码、内容类型或其他标题。一旦你有了连接,R中的许多其他函数就会接受.您可以使用各种read.csv(x)、read.delim(x)、scan(x)等等。
library('curl');
h <- new_handle();
x <- curl('https://cran.r-project.org', handle=h);
open(x);
handle_data(h)$status_code;
handle_data(h)$type;
parse_headers(handle_data(h)$headers);
y <- readLines(x);
close(x);https://stackoverflow.com/questions/73391442
复制相似问题