URLFetchAsynchronous["url",func]
在后台中执行链接,当发起事件时,调用 func.
URLFetchAsynchronous
URLFetchAsynchronous["url",func]
在后台中执行链接,当发起事件时,调用 func.
更多信息和选项
- 可能发起的事件类型:
-
"data" 返回字节列表,并且表明连接已经完成 "progress" 返回关于形如 {dlnow,dltotal,ulnow,ultotal} 的当前链接的信息 "error" 表明当尝试连接到 URL 时出现错误 "headers" 从网络服务器接收到的头部 "cookies" 所有已知 cookies 的列表 "statuscode" 服务器所返回的状态代码 - 可以给出下列选项:
-
Method "GET" 用于请求的方法 "Parameters" {} 发送给请求的参数 "Body" "" 发送的消息体内容 "MultipartElements" {} 发送的多部分数据元素的列表 "Username" "" 用于请求的用户名 "Password" "" 用于请求的密码 "Headers" {} 手动发送给 HTTP 服务器的头部 "Cookies" Automatic 是否存储接收到的 cookie "StoreCookies" True 传递给服务器的cookie "VerifyPeer" True 使用 SSL 证明验证身份 "UserAgent" Automatic 发送的用户代理字符串 "ReadTimeout" 0 允许用于上传或者下载数据的时间 "ConnectTimeout" 0 允许连接服务器的时间 "Progress" False 是否发起 "progress" 事件 "Transfer" Automatic 如果 Automatic,"data" 事件返回一次,并且返回所有下载的数据;如果 "Chunks",将从上一次事件开始多次发起具有下载数据的 "data"事件 "UserData" None 传递给该选项的任意表达式将存储用于在事件函数内使用的AsynchronousTaskObject;数据可以通过检查传递给事件函数的选项 AsynchronousTaskObject 找到 "FollowRedirects" True 是否重定向 - 默认情况下,允许的时间以秒为单位给出.
- 多部分元素的格式为 {"name","mimetype"}->val,其中 val 是一个字符串或者字节列表.
- 当发起一个事件时,将执行传递给 URLFetchAsynchronous 的事件函数 . 将有三个参数被传递给事件函数,分别是:AsynchronousTaskObject、事件名称和从事件获取到的数据. 事件函数是处理从 URLFetchAsynchronous 获取的数据的唯一方法.
范例
打开所有单元 关闭所有单元基本范例 (3)
url = "wolfram.com";
func = Function[{asyncObj, eventType, document}, If[eventType === "data", MessageDialog["Finished downloading " <> url]]];
URLFetchAsynchronous[url, func]headers = {};
cookies = {};
eventFunction[_, "headers", data_] := headers = data
eventFunction[_, "cookies", data_] := cookies = data
URLFetchAsynchronous["wolfram.com", eventFunction]headers
cookiesstatusList = {};
statusFunction[async_, "statuscode", status_] := AppendTo[statusList, {async, status}]
statusFunction[async_, "error", errorStr_] := AppendTo[statusList, {async, errorStr}]
URLFetchAsynchronous["http://wolfram.com", statusFunction]
URLFetchAsynchronous["http://wolframalpha.com", statusFunction]
URLFetchAsynchronous["http://notreal", statusFunction]statusList选项 (16)
"Method" (1)
"Parameters" (1)
"VerifyPeer" (1)
"Username" (1)
"Password" (1)
"UserAgent" (1)
"Cookies" (1)
手动控制 URLFetchAsynchronous 所使用的 cookie:
contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/cookies.php", eventFunction, "Cookies" -> {{"Domain" -> ".exampledata.wolfram.com", "Path" -> "/", "Secure" -> "FALSE", "Expires" -> DateString[{2020, 1, 1, 0, 0, 0}], "Name" -> "MyCookie", "Value" -> "CookieData"}}]FromCharacterCode[contentData]"StoreCookies" (1)
如果 False,当链接到全局 cookie share 中的站点时,"StoreCookies" 不会放置找到的任何 cookie:
cookiesData;
eventFunction[_, "cookies", cookies_] := cookiesData = cookies
URLFetchAsynchronous["http://wolfram.com/", eventFunction, "StoreCookies" -> False]cookiesData$HTTPCookies"Headers" (1)
"BodyData" (1)
"MultipartData" (1)
contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/multipartdata.php", eventFunction, "MultipartData" -> {{"partName", "mime/type", {1, 2, 3, 4, 5}}}]FromCharacterCode[contentData]"ReadTimeout" (1)
"ConnectTimeout" (1)
"Progress" (1)
如果 True,将发起 "progress" 事件,返回下载和上传的进度信息:
progressData = {};progressFunction[_, "progress", progress_] := progressData = progress
URLFetchAsynchronous["http://exampledata.wolfram.com/5mb.dat", progressFunction, "Progress" -> True];
Dynamic[progressData]"Transfer" (1)
属性和关系 (1)
使用 URLSaveAsynchronous 把输出存储在文件中:
f[_, "statuscode", _] := CreateDialog[{"Finished download", DefaultButton[]}]
URLSaveAsynchronous["http://wolfram.com", FileNameJoin[{$TemporaryDirectory, "index.html"}], f]文本
Wolfram Research (2012),URLFetchAsynchronous,Wolfram 语言函数,https://reference.wolfram.com/language/ref/URLFetchAsynchronous.html.
CMS
Wolfram 语言. 2012. "URLFetchAsynchronous." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/URLFetchAsynchronous.html.
APA
Wolfram 语言. (2012). URLFetchAsynchronous. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/URLFetchAsynchronous.html 年
BibTeX
@misc{reference.wolfram_2026_urlfetchasynchronous, author="Wolfram Research", title="{URLFetchAsynchronous}", year="2012", howpublished="\url{https://reference.wolfram.com/language/ref/URLFetchAsynchronous.html}", note=[Accessed: 01-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_urlfetchasynchronous, organization={Wolfram Research}, title={URLFetchAsynchronous}, year={2012}, url={https://reference.wolfram.com/language/ref/URLFetchAsynchronous.html}, note=[Accessed: 01-November-2026]}