ofURLFileLoader (functions)
ofLoadURL( ... )
ofHttpResponse ofLoadURL(const string &url)make an HTTP GET request blocks until a response is returned or the request times out
Parameters:
url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
Returns: HTTP response
Loads content from the specified URL. It makes a synchronous HTTP request and returns the response as an instance of the ofHttpResponse class.
For example, this will retrieve the contents of a text file and print the output to the console.
ofHttpResponse resp = ofLoadURL("http://www.google.com/robots.txt");
cout << resp.data << endl;
ofLoadURLAsync( ... )
int ofLoadURLAsync(const string &url, const string &name)make an asynchronous HTTP GET request will not block, placed in a queue and run using a background thread
Parameters:
url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
name optional key to use when sorting requests \return unique id for the active HTTP request
Loads content asynchronously from the specified URL and
returns the ID of the process. You need to listen for URL notifications
in testApp::urlResponse(ofHttpResponse&)
Step 1. Declare urlResponse in the header of the class which should receive notifications:
class testApp : public ofBaseApp {
public:
void urlResponse(ofHttpResponse & response);
}
Step 2. Define urlResponse in the class which should receive notifications:
void testApp::urlResponse(ofHttpResponse & response) {
if (response.status==200 && response.request.name == "async_req") {
img.loadImage(response.data);
loading = false;
} else {
cout << response.status << " " << response.error << endl;
if (response.status != -1) loading = false;
}
}
Step 3. Enable URL notifications
void testApp::setup() {
ofRegisterURLNotification(this);
}
Step 4. Submit the asynchronous request
int id = ofLoadURLAsync("http://www.openframeworks.cc/images/ofw-logo.png",
"async_req");
Examples based on http://www.slideshare.net/roxlu/openframworks-007-utils
ofRegisterURLNotification( ... )
void ofRegisterURLNotification(T *obj)Registers a listener to receive notifications from ofLoadURLAsync() .
void testApp::setup() {
ofRegisterURLNotification(this);
}
ofRemoveAllURLRequests( )
void ofRemoveAllURLRequests()remove all active HTTP requests from the queue
Removes all asynchronously loaded URL requests initiated by
ofLoadURLAsync() .
ofRemoveURLRequest( ... )
void ofRemoveURLRequest(int id)remove an active HTTP request from the queue
Parameters:
unique HTTP request id
Removes a single request initiated by ofLoadURLAsync() . The request is
specified by its ID.
ofSaveURLAsync( ... )
int ofSaveURLAsync(const string &url, const filesystem::path &path)make an asynchronous HTTP request and save the response data to a file will not block, placed in a queue and run using a background thread
Parameters:
url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
path file path to save to
Returns: unique id for the active HTTP request
Asynchronously saves a file from a URL. The returned int is the id of the request. This allows you to remove the request if it keeps failing, and also to identify when it has finished.
ofSaveURLTo( ... )
ofHttpResponse ofSaveURLTo(const string &url, const filesystem::path &path)make an HTTP GET request and save the response data to a file blocks until a response is returned or the request times out
Parameters:
url HTTP url to request, ie. "http://somewebsite.com/someapi/someimage.jpg"
path file path to save to \return HTTP response on success or failure
Retrieves a file from a remote URL and saves it locally. This is a synchronous method.
See also: ofSaveURLAsync()
ofStopURLLoader( )
void ofStopURLLoader()stop & remove all active and waiting HTTP requests
ofURLResponseEvent( )
int & ofURLResponseEvent()Used internally for registering and unregistering URL notifications, and
also by ofThreadedImageLoader and ofURLFileLoader.
ofUnregisterURLNotification( ... )
void ofUnregisterURLNotification(T *obj)Unregisters a notification for an ofLoadURLAsync() operation.