| View previous topic :: View next topic |
| Author |
Message |
ChrisMims
Joined: 29 Dec 2009 Posts: 7
|
Posted: Tue Dec 29, 2009 5:07 am Post subject: Downloading a Text File |
|
|
Can anyone tell me how to download "file1.txt" from "examplesite.com/file1.txt", and have it placed in "ms0/psp/game/MyGame"?
Please help, and thanks in advance,
-ChrisMims |
|
| Back to top |
|
 |
gotmilk065
Joined: 28 Nov 2009 Posts: 25
|
Posted: Tue Dec 29, 2009 11:26 am Post subject: |
|
|
| Use libcurl if you want it easy :D |
|
| Back to top |
|
 |
ChaoticXSinZ
Joined: 30 Nov 2009 Posts: 4
|
Posted: Tue Dec 29, 2009 1:54 pm Post subject: libCurl Example |
|
|
As the above poster said, you can use libCurl to easily download files.
First you have to make sure you're actually connected to the internet. You can manually connect to Access points or use the net dialog. You can find examples in the pspsdk samples folder.
Here is an example download function:
| Code: | /**
* Write for cUrl response.
*/
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData) {
if (writerData == NULL)
return false;
writerData->append(data, size * nmemb);
return size * nmemb;
}
/**
* Download a file using cURL.
*
* @param std::string url The url to download from.
* @param std::string &response Pointer to string object to populate with response. (Which can also be error message.)
*
* @return int If -1 then error, other wise your good.
*/
int downloadFile(std::string url, std::string &response) {
// Error buffer
char errorBuffer[CURL_ERROR_SIZE];
// cURL Handle
CURL *curl = curl_easy_init();
// cURL Code
CURLcode res;
// HTTP Header list
struct curl_slist *headersList = NULL;
// Make sure we have a valid handle
if (curl != NULL) {
// Setup error buffer
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
// Set Options:
// Allow following 'Location' headers
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// Set URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Add User Agent to headers list
headersList = curl_slist_append(headersList, "MyPSPApp/1.0");
// Set Connection to close
headersList = curl_slist_append(headersList, "Connection: Close");
// Set encoding to ALL
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
// Set HTTP Headers
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headersList);
// Set writer function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
// Set write buffer
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Perform request
res = curl_easy_perform(curl);
// Error
if (res != CURLE_OK) {
// Set response to error message
response.assign(errorBuffer);
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return -1;
}
} else {
// Unable to create curl connection
// Set response to error message
response.assign("Unable to create cURL connection.");
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return -1;
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headersList);
return 1;
} |
You would then do:
| Code: | std::string data;
int res = downloadFile("http://example.com/file.txt", data);
if (data != -1) {
// Use data, save to file etc
} else {
// Handle error
} |
This assumes you're using C++ and have already setup curl, included the headers etc. |
|
| Back to top |
|
 |
ChrisMims
Joined: 29 Dec 2009 Posts: 7
|
Posted: Thu Dec 31, 2009 1:11 am Post subject: |
|
|
| Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib? |
|
| Back to top |
|
 |
ChrisMims
Joined: 29 Dec 2009 Posts: 7
|
Posted: Sun Jan 10, 2010 3:43 am Post subject: |
|
|
| Where can I find the libraries for the Curl functions? Also, is there a way to do this with OSLib? |
|
| Back to top |
|
 |
Alberto
Joined: 12 Feb 2007 Posts: 57 Location: Sofia
|
Posted: Sun Jan 10, 2010 7:09 am Post subject: |
|
|
| ChrisMims wrote: | | (...)Also, is there a way to do this with OSLib? |
just because I was looking at OSLib in the past days...
OSLib is just a graphic layer, has nothing to do with Curl... so what's the point?
A. |
|
| Back to top |
|
 |
ChrisMims
Joined: 29 Dec 2009 Posts: 7
|
Posted: Sun Jan 10, 2010 7:44 am Post subject: |
|
|
| Ok. How do I get the Curl libraries? |
|
| Back to top |
|
 |
whistler
Joined: 04 Mar 2008 Posts: 40
|
|
| Back to top |
|
 |
|