class methods
- ofURLFileLoader()
- clear()
- get()
- getAsync()
- handleRequest()
- handleRequestAsync()
- remove()
- saveAsync()
- saveTo()
- stop()
global functions
This class provides several convenient methods for making HTTP requests.
ofHttpResponse resp = ofLoadURL("http://www.google.com/robots.txt");
cout << resp.data << endl;
Documentation from code comments
loads a file from a URL using an HTTP request
clear()
void ofURLFileLoader::clear()
Documentation from code comments
clear all active HTTP requests from the queue
get(...)
ofHttpResponse ofURLFileLoader::get(const string &url)
Documentation from code comments
make an HTTP 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" \return HTTP response on success or failure
getAsync(...)
int ofURLFileLoader::getAsync(const string &url, const string &name)
Documentation from code comments
make an asynchronous HTTP 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
handleRequest(...)
ofHttpResponse ofURLFileLoader::handleRequest(const ofHttpRequest &request)
Documentation from code comments
blocks until a response is returned or the request times out \return HTTP response on success or failure
handleRequestAsync(...)
int ofURLFileLoader::handleRequestAsync(const ofHttpRequest &request)
Documentation from code comments
this is a non-blocking version of handleRequest that will return a response in the urlResponse callback \return unique id of the active HTTP request
remove(...)
void ofURLFileLoader::remove(int id)
Documentation from code comments
remove an active HTTP request from the queue
Parameters:
unique HTTP request id
saveAsync(...)
int ofURLFileLoader::saveAsync(const string &url, const filesystem::path &path)
Documentation from code comments
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
saveTo(...)
ofHttpResponse ofURLFileLoader::saveTo(const string &url, const filesystem::path &path)
Documentation from code comments
make an HTTP 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
stop()
void ofURLFileLoader::stop()
Documentation from code comments
stop & remove all active and waiting HTTP requests
ofLoadURL(...)
ofHttpResponse ofLoadURL(const string &url)
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;
Documentation from code comments
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
ofLoadURLAsync(...)
int ofLoadURLAsync(const string &url, const string &name)
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
Documentation from code comments
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
ofRegisterURLNotification(...)
void ofRegisterURLNotification(T *obj)
Registers a listener to receive notifications from ofLoadURLAsync()
.
void testApp::setup() {
ofRegisterURLNotification(this);
}
ofRemoveAllURLRequests()
void ofRemoveAllURLRequests()
Removes all asynchronously loaded URL requests initiated by
ofLoadURLAsync()
.
Documentation from code comments
remove all active HTTP requests from the queue
ofRemoveURLRequest(...)
void ofRemoveURLRequest(int id)
Removes a single request initiated by ofLoadURLAsync()
. The request is
specified by its ID.
Documentation from code comments
remove an active HTTP request from the queue
Parameters:
unique HTTP request id
ofSaveURLAsync(...)
int ofSaveURLAsync(const string &url, const filesystem::path &path)
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.
Documentation from code comments
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
ofSaveURLTo(...)
ofHttpResponse ofSaveURLTo(const string &url, const filesystem::path &path)
Retrieves a file from a remote URL and saves it locally. This is a synchronous method.
See also: ofSaveURLAsync()
Documentation from code comments
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
ofStopURLLoader()
void ofStopURLLoader()
Documentation from code comments
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.
Last updated 화요일, 19 11월 2024 17:25:50 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue