Torna al Thread
private bool downloadFile(string host, string param, string fileDest, string destDir, string DataDeiTitoli, string type)
{
bool toReturn = false;
string parametri = param;
parametri = parametri.Replace("%type%", type);
parametri = parametri.Replace("_", "&");
parametri = parametri.Replace("%date%", DataDeiTitoli);
string url = host;
string fileName = fileDest;
string folderDest = destDir;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parametri);
Stream os = null;
try
{
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
toReturn = true;
}
catch (WebException ex)
{
toReturn = false;
Logger.App.Write(GetType(), Severity.Warning, "Errore nell'acquisizione da remoteServer", ex);
}
finally
{
if (os != null)
{
os.Close();
}
}
if (toReturn)
{
try
{ // get the response
HttpWebResponse webResponse = HttpWebResponse)webRequest.GetResponse();
Stream strRemote = null;
Stream strLocal = null;
if (webResponse == null)
{ return false; }
string path = folderDest + fileName;
using (WebClient wcDownload = new WebClient())
{
try
{
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strRemote = webResponse.GetResponseStream();
// Create a new file stream where we will be saving the data (local drive)
strLocal = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
// It will store the current number of bytes we retrieved from the server
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strRemote.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
}
}
finally
{
// When the above code has ended, close the streams
strRemote.Close();
strLocal.Close();
}
}
}
catch (WebException ex)
{
toReturn = false;
Logger.App.Write(GetType(), Severity.Error, "HttpPost: Response error", ex);
}
}
return toReturn;
}