({{, URLFetchAsynchronous[url,func] 
performs a connection in the background, calling func when an event is raised.}})

OpenersNotesSection(Spacer1)Details and Options

Types of events that may be raised:

The following options can be given:

Allowed times are by default given in seconds.

Multipart elements must be of the form {"name","mimetype"}->val, where val is a string or a list of bytes.

When an event is raised, the event function passed to URLFetchAsynchronous will be executed. The event functions will be passed three arguments, the (RefLink[AsynchronousTaskObject, paclet:ref/AsynchronousTaskObject]), the event name, and the data received from the event. Event functions are the only method for handling data received from URLFetchAsynchronous.

OpenersPrimaryExamplesSection(Spacer1)Examples  (20)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

This opens a dialog when the download has finished:

url = "wolfram.com";
func = Function[{asyncObj, eventType, document}, If[eventType==="data", MessageDialog["Finished downloading " <> url]]];
URLFetchAsynchronous[url, func]

Store headers and cookies:

headers={};
cookies={};
eventFunction[_, "headers", data_]:=headers = data
eventFunction[_, "cookies", data_]:=cookies=data
URLFetchAsynchronous["wolfram.com", eventFunction]

headers
cookies

Use the status code and error event to determine the availability of URLs:

statusList={};
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

OpenersExampleSection1(Spacer1)Options  (16)

OpenersExampleSubsection0(Spacer1)"Method"  (1)

"Method" can be used to specify the HTTP method used by the connection:

headerData;
eventFunction[_, "headers", headers_]:=headerData = headers
URLFetchAsynchronous["http://wolfram.com",eventFunction,"Method"->"HEAD"]

headerData

OpenersExampleSubsection1(Spacer1)"Parameters"  (1)

Specify the parameters that should be sent to the server:

contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/httpget.php",eventFunction, "Parameters"->{"name"->"Joe", "age"->"30"}]

FromCharacterCode[contentData]

OpenersExampleSubsection2(Spacer1)"VerifyPeer"  (1)

Verify that the SSL certificate used by the server is valid:

statusData;
eventFunction[_, "statuscode", status_]:=statusData = status;
URLFetchAsynchronous["https://wolfram.com", eventFunction,"VerifyPeer"->True]

statusData

OpenersExampleSubsection3(Spacer1)"Username"  (1)

Specify the username that should be sent to the server:

contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/authentication.php",eventFunction, "Username"->"Joe", "Password"->""]

FromCharacterCode[contentData]

OpenersExampleSubsection4(Spacer1)"Password"  (1)

Specify the password that should be sent to the server:

contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/authentication.php",eventFunction, "Username"->"Joe","Password"->"topsecret"]

FromCharacterCode[contentData]

OpenersExampleSubsection5(Spacer1)"UserAgent"  (1)

Specify the user agent that should be sent to the server:

contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/useragent.php",eventFunction, "UserAgent"->"Web Browser"]

FromCharacterCode[contentData]

OpenersExampleSubsection6(Spacer1)"Cookies"  (1)

Manually control the cookies used by URLFetchAsynchronous:

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]

OpenersExampleSubsection7(Spacer1)"StoreCookies"  (1)

If (RefLink[False, paclet:ref/False]), "StoreCookies" will not place any new cookies found while connecting to the site in the global cookie share:

cookiesData;
eventFunction[_, "cookies", cookies_]:=cookiesData = cookies
URLFetchAsynchronous["http://wolfram.com/", eventFunction, "StoreCookies"->False]

cookiesData

$HTTPCookies

OpenersExampleSubsection8(Spacer1)"Headers"  (1)

Specify headers to be sent to the server:

contentData;
eventFunction[_, "data", headers_]:= contentData = headers
URLFetchAsynchronous["http://exampledata.wolfram.com/headers.php",eventFunction, "Headers"->{"MyHeader"->"42"}]

FromCharacterCode[contentData]

OpenersExampleSubsection9(Spacer1)"BodyData"  (1)

Specify the message body that should be sent when connecting to the server:

contentData;
eventFunction[_, "data", data_] := contentData = data
URLFetchAsynchronous["http://exampledata.wolfram.com/bodydata.php", eventFunction,"BodyData"->"Message body data sent to server."]

FromCharacterCode[contentData]

OpenersExampleSubsection10(Spacer1)"MultipartData"  (1)

Specify the contents of a multipart message body:

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]

OpenersExampleSubsection11(Spacer1)"ReadTimeout"  (1)

Specify the maximum allotted time in seconds to finish downloading data from the server:

errorString;
eventFunction[_, "error", error_] := errorString = error
URLFetchAsynchronous["http://exampledata.wolfram.com/50mb.dat", eventFunction,"ReadTimeout"->1]

errorString

OpenersExampleSubsection12(Spacer1)"ConnectTimeout"  (1)

Specify the maximum allotted time in seconds to establish a connection to the server:

errorString;
eventFunction[_, "error", error_] := errorString = error
URLFetchAsynchronous["http://127.0.0.1", eventFunction,"ConnectTimeout"->1]

errorString

OpenersExampleSubsection13(Spacer1)"Progress"  (1)

If (RefLink[True, paclet:ref/True]), the "progress" event will be raised, returning information downloaded and uploaded about in-progress downloads:

progressData={};

progressFunction[_, "progress", progress_]:=progressData =progress
URLFetchAsynchronous["http://exampledata.wolfram.com/5mb.dat",progressFunction,"Progress"->True];
Dynamic[progressData]

OpenersExampleSubsection14(Spacer1)"Transfer"  (1)

If "Transfer" is set to "Chunks", data will be returned as soon as it is available from the server, instead of waiting for the entire connection to finish:

chunks={};

chunkFunction[_, "data", data_]:=AppendTo[chunks, data]
URLFetchAsynchronous["http://wolfram.com",chunkFunction,"Transfer"->"Chunks"];

Length[chunks]

OpenersExampleSubsection15(Spacer1)"UserData"  (1)

"UserData" is any expression containing information to be made available when an event is called:

dataString;
eventFunction[async_, "data", data_]:=dataString="UserData"/.Options[async]
URLFetchAsynchronous["http://wolfram.com",eventFunction,"UserData"->"stored data"];

dataString

OpenersExampleSection2(Spacer1)Properties & Relations  (1)

Use (RefLink[URLSaveAsynchronous, paclet:ref/URLSaveAsynchronous]) to store output in a file:

f[_,"statuscode",_]:=CreateDialog[{"Finished download", DefaultButton[]}]
URLSaveAsynchronous["http://wolfram.com",FileNameJoin[{$TemporaryDirectory,"index.html"}],f]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}})}})