That working example is a webserver, and you want to be a client to retrieve a file from a server, right ?
The setup function is to initialize things, and the loop() is for the normal working code.
Do you want to retrieve that file just once, then I would place that code in setup().
Do you want to retrieve that file every hour or every day, then I would place that code in loop().
I have a working sketch to retrieve a file, but it is in development, and has my passwords and everything.
I started with this: Read a text file from internet site - Project Guidance - Arduino Forum
My sketch below is a stripped-down version. Perhaps you can compare it step by step with your sketch. When your sketch will compile, show your new sketch to us.
//
// Getting a file from internet
// (Everything with passwords and other things are removed)
//
// Arduino IDE 1.5.8 BETA, Arduino Mega 2560 board + Ethernet Shield.
//
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// Generated mac with : http://www.miniwebtool.com/mac-address-lookup/
byte mac[] = { 0xDE, 0x36, 0x5F, 0x0A, 0x19, 0x83 }; //physical mac address
EthernetClient client;
void setup()
{
Serial.begin( 9600);
Serial.println(F( "\nGET'n a file from internet sketch"));
// SPI
// ---
// Set SS (pin 53 on the Mega) as output.
// Disable the SPI chip selects, by setting them high.
// On the Arduino Mega, the SS pin is pin 53.
// This must be an output for the SPI to work, although pin 53
// is not used.
pinMode( SS, OUTPUT); // SS = 10 on Uno, SS = 53 on the Mega
digitalWrite( SS, HIGH); // disable anything at the default SS
pinMode( 4, OUTPUT);
digitalWrite( 4, HIGH); // disable SD
pinMode( 10, OUTPUT);
digitalWrite(10, HIGH); // disable W5100
delay(1);
if( Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address.
Serial.println(F( "Ethernet started."));
Serial.print(F( "Local IP = "));
Serial.println( Ethernet.localIP());
// See if the card is present and can be initialized:
if (!SD.begin( 4))
{
Serial.println("SD card failed, or not present");
// don't do anything more:
while(true);
}
Serial.println("SD card initialized.");
}
void loop()
{
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'i') // checks to see byte is an i
{
GetAndStore();
}
}
}
void GetAndStore() //client function for GET request data.
{
if( !client.connect(<server_ip>,80))
{
Serial.println("connection failed"); //error message if no client connect
}
else
{
//starts client connection
Serial.println("connected");
// It depends on the server how little or how much information is needed for the GET.
client.println("GET /index.html HTTP/1.1");
client.println("Connection: close");
client.println();
// The reponse is 5..12 lines, each with CR LF (0x0D, 0x0A) at the end.
// An empty line indicates the start of the data.
client.find("Content-Length: ");
long len = client.parseInt();
Serial.print("Content-Length=");
Serial.println( len);
// For normal servers, "\r\n\r\n" is the string to search for.
client.find("\r\n\r\n");
// The requested data has been read until the empty line.
// The data that follows is the file that was requested.
// Do a few tests before creating a file.
if( client.connected() && client.available() > 0 && len > 0)
{
// Open a file (the name in 8.3 format)
char fileName[16] = "myfile.htm"
File dataFile = SD.open(fileName, FILE_WRITE);
Serial.print("Writing file ");
Serial.println( fileName);
long fileSize = 0L;
// if the file is available, write to it:
if (dataFile)
{
// Wait up to a total of 10 seconds to get the file.
unsigned long previous = millis();
// "len" is the "Content-Length".
// Reading is done in chunks that are available.
// fileSize keeps track of the read bytes.
while( millis() - previous < 10000UL && client.connected() && fileSize < len)
{
int n = client.available();
for( int i=0; i<n; i++)
{
byte c = client.read(); // get byte from ethernet buffer
dataFile.write( c); // write raw data
}
fileSize += n;
}
dataFile.close();
Serial.print("Ready writing ");
Serial.print( fileSize);
Serial.println( " bytes");
}
else
{
// if the file isn't open, pop up an error:
Serial.println(F("error opening file"));
}
}
else
{
Serial.println(F( "Client was not connected or not available or len was zero."));
}
}
client.stop(); //stop client
}