Unity 3D simple C# file send to FTP server sample script - c#

I'm new to unity, I need a simple script to send a XML file (don't need to read contents) from "StreamingAssets" folder to our FTP server root folder with ability to change |"FTP User Name" "FTP Password" "FTP Host Name" "FTP Port"|. I have been seen some example in unity forums and unity documentation but nothing helped me. if you know simple ways please guide me thanks.
I found this but it doesn't work.
using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.IO;
public class Uploader : MonoBehaviour
{
public string FTPHost = "ftp.byethost7.com";
public string FTPUserName = "b7_18750253";
public string FTPPassword = "**********";
public string FilePath;
public void UploadFile()
{
WebClient client = new System.Net.WebClient();
Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);
Debug.Log (uri);
client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
client.UploadFileAsync(uri, "STOR", FilePath);
}
void start()
{
FilePath = Application.dataPath+"/StreamingAssets/data.xml";
UploadFile ();
}
}

Change this:
Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);
With this:
Uri uri = new Uri(FTPHost + "/" + new FileInfo(FilePath).Name);
The method OnFileUploadCompleted shows that the URI hasn't "/".
The Uri constructor seemingly joins the file name with the path name without format. (Works for me in Unity 2018.2.3f1). There are other ways to format a url, I just try point the error.
Worst English ever, I know... :D

You are simply not calling the UploadFile() function from anywhere. Because it is a public function, I assumed it is being called from another script, but since the Debug.Log (uri); code is not showing anything,it is likely that the function is not being called at-all. You must call it from somewhere for it to run.
For testing purposes, call it from the Start() function. Add the code below to your script.
void Start()
{
UploadFile();
}
Note that you must attach the Uploader script to a GameObject in your scene. That GameObject must be enabled in order for your Start() function to be called.
EDIT:
Even when you do this, you will get error that says:
The format of the URI could not be determined: blah blah blah
Your "ftp.byethost7.com"; link should be "ftp://byethost7.com";
Here is a complete FTP upload code in Unity.
using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.IO;
public class Uploader : MonoBehaviour
{
public string FTPHost = "ftp://byethost7.com";
public string FTPUserName = "b7_18750253";
public string FTPPassword = "xxx";
public string FilePath;
public void UploadFile()
{
FilePath = Application.dataPath + "/StreamingAssets/data.xml";
Debug.Log("Path: " + FilePath);
WebClient client = new System.Net.WebClient();
Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);
client.UploadProgressChanged += new UploadProgressChangedEventHandler(OnFileUploadProgressChanged);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(OnFileUploadCompleted);
client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
client.UploadFileAsync(uri, "STOR", FilePath);
}
void OnFileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
Debug.Log("Uploading Progreess: " + e.ProgressPercentage);
}
void OnFileUploadCompleted(object sender, UploadFileCompletedEventArgs e)
{
Debug.Log("File Uploaded");
}
void Start()
{
UploadFile();
}
}

using System.IO;
public void UploadFile2()
{
FilePath = Application.dataPath + "/StreamingAssets/ARITMETICA3DAZ1.txt";
Debug.Log("Path: " + FilePath);
Uri uri = new Uri(FTPHost + "/" + new FileInfo(FilePath).Name);
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential (FTPUserName,FTPPassword);
state.Request = request;
state.FileName = FilePath;
request.BeginGetRequestStream(
//new AsyncCallback (EndGetStreamCallback),
new AsyncCallback (EndGetStreamCallback),
state
);
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;
public FtpState()
{
wait = new ManualResetEvent(false);
}
public ManualResetEvent OperationComplete
{
get {return wait;}
}
public FtpWebRequest Request
{
get {return request;}
set {request = value;}
}
public string FileName
{
get {return fileName;}
set {fileName = value;}
}
public Exception OperationException
{
get {return operationException;}
set {operationException = value;}
}
public string StatusDescription
{
get {return status;}
set {status = value;}
}
}
public class AsynchronousFtpUpLoader
{
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;
Uri target = new Uri (args[0]);
string fileName = args[1];
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example uses anonymous logon.
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
// Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName;
// Get the event to wait on.
waitObject = state.OperationComplete;
// Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback (EndGetStreamCallback),
state
);
// Block the current thread until all operations are complete.
waitObject.WaitOne();
// The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine ("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback (EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
}
}
// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse) state.Request.EndGetResponse(ar);
response.Close();
state.StatusDescription = response.StatusDescription;
// Signal the main application thread that
// the operation is complete.
state.OperationComplete.Set();
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine ("Error getting response.");
state.OperationException = e;
state.OperationComplete.Set();
}
}
}
}

Related

Upload image to a FTP Server using PCL Xamarin Forms

i'm new on Xamarin and C# world and i'm trying to upload an Image to a FTP Server. I saw the FtpWebRequest class to do this but i'm not getting it right, i don't know how to inject plataform specific code and i don't even know what it really mean, already watched this video (https://www.youtube.com/watch?feature=player_embedded&v=yduxdUCKU1c) but i don't see how to use this to create the FtpWebRequest class and upload the image.
I Saw this code(here: https://forums.xamarin.com/discussion/9052/strange-behaviour-with-ftp-upload) to send a picture and i'm unable to use it.
public void sendAPicture(string picture)
{
string ftpHost = "xxxx";
string ftpUser = "yyyy";
string ftpPassword = "zzzzz";
string ftpfullpath = "ftp://myserver.com/testme123.jpg";
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
//userid and password for the ftp server
ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(picture);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
ftpstream.Flush();
// fs.Flush();
}
I don't have a type FileStream, WebRequestMethods and File, also my FtpWebRequest class doens't have "KeepAlive", "UseBinary" and "GetRequestStream" methods, and my Stream class doesn't have "Close" method.
My FtpWebRequest Class:
public sealed class FtpWebRequest : WebRequest
{
public override string ContentType
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override WebHeaderCollection Headers
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override string Method
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override Uri RequestUri
{
get
{
throw new NotImplementedException();
}
}
public override void Abort()
{
throw new NotImplementedException();
}
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
{
throw new NotImplementedException();
}
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
throw new NotImplementedException();
}
public override Stream EndGetRequestStream(IAsyncResult asyncResult)
{
throw new NotImplementedException();
}
public override WebResponse EndGetResponse(IAsyncResult asyncResult)
{
throw new NotImplementedException();
}
}
(I know, i didn't wrote anything there, just pressed ctrl + . because i don't know what to write there)
Does anyone can provide me a full sample of a FtpWebRequest class? i only find the class in use like this one above.
Ok, I just figured out how to do this and I'll show how I did, I don't really know if is the better and right way to do, but it works.
First I had to create a Interface Class Called IFtpWebRequest on my forms project, which contains exactly this:
namespace Contato_Vistoria
{
public interface IFtpWebRequest
{
string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "");
}
}
Then, inside my iOS/droid project i had to create a class caled FTP that implements IFtpWebRequest and inside that class i wrote the upload function(I'm using another one now), here's the ENTIRE FTP class:
using System;
using System.IO;
using System.Net;
using Contato_Vistoria.Droid; //My droid project
[assembly: Xamarin.Forms.Dependency(typeof(FTP))] //You need to put this on iOS/droid class or uwp/etc if you wrote
namespace Contato_Vistoria.Droid
{
class FTP : IFtpWebRequest
{
public FTP() //I saw on Xamarin documentation that it's important to NOT pass any parameter on that constructor
{
}
/// Upload File to Specified FTP Url with username and password and Upload Directory if need to upload in sub folders
///Base FtpUrl of FTP Server
///Local Filename to Upload
///Username of FTP Server
///Password of FTP Server
///[Optional]Specify sub Folder if any
/// Status String from Server
public string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
{
try
{
string PureFileName = new FileInfo(fileName).Name;
String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(userName, password);
req.UseBinary = true;
req.UsePassive = true;
byte[] data = File.ReadAllBytes(fileName);
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
return res.StatusDescription;
}
catch(Exception err)
{
return err.ToString();
}
}
}
}
It's pretty much the same on my iOS project, but I'll post it anyway to help those like me that don't know too much and need to see full examples of how to do it. Here it is:
using System;
using System.Net;
using System.IO;
//Only thing that changes to droid class is that \/
using Foundation;
using UIKit;
using Contato_Vistoria.iOS;
[assembly: Xamarin.Forms.Dependency(typeof(FTP))]
namespace Contato_Vistoria.iOS // /\
{
class FTP : IFtpWebRequest
{
public FTP()
{
}
/// Upload File to Specified FTP Url with username and password and Upload Directory if need to upload in sub folders
///Base FtpUrl of FTP Server
///Local Filename to Upload
///Username of FTP Server
///Password of FTP Server
///[Optional]Specify sub Folder if any
/// Status String from Server
public string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
{
try
{
string PureFileName = new FileInfo(fileName).Name;
String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(userName, password);
req.UseBinary = true;
req.UsePassive = true;
byte[] data = File.ReadAllBytes(fileName);
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
return res.StatusDescription;
}
catch (Exception err)
{
return err.ToString();
}
}
}
}
Finally, back on my Xamarin Forms project, this is how i called the function. Inside a simple click event from a Button on my GUI:
protected async void btConcluidoClicked(object sender, EventArgs e)
{
if (Device.OS == TargetPlatform.Android || Device.OS == TargetPlatform.iOS)
await DisplayAlert("Upload", DependencyService.Get<IFtpWebRequest>().upload("ftp://ftp.swfwmd.state.fl.us", ((ListCarImagesViewModel)BindingContext).Items[0].Image, "Anonymous", "gabriel#icloud.com", "/pub/incoming"), "Ok");
await Navigation.PopAsync();
}
To call the function you need to write "DependencyService.Get().YourFunction(Parameters of the function)", to be more specific.
And that's how I did it, hope I can help someone.

How to go to start of Stream to avoid two webrequests?

I have a small C# console app working that copies the results of a webrequest to a text file and then runs each command in that text file, saving the results to a separate text file.
Problem is, I have to make two requests to the same server, which I don't like doing. The problem is I can't seem to go to the beginning of the Stream/StreamReader after writing it to the text file, forcing me to make another request.
How do I do this with only one webrequest?
Thanks,
John
static void Main(string[] args)
{
// Set all variables
string epoUrl = "https://de-ser2012ecm:8443/remote/core.help.do";
string commandHelpPath = #"C:\Logs\AllCommandsHelp.txt";
string coreHelpPath = #"C:\Logs\CoreHelp.txt";
string epoUsername = "admin";
string epoPassword = "password";
string responseFromServer;
StringReader strReader;
try
{
// Get stream from webrequest
Stream coreStream = WebHelper.GetWebResponseStream(epoUrl, epoUsername, epoPassword);
StreamReader coreReader = new StreamReader(coreStream);
// Write core help page to text file
using (StreamWriter corefile = new StreamWriter(coreHelpPath, true, Encoding.UTF8))
{
responseFromServer = coreReader.ReadToEnd();
// Display the content.
corefile.Write(responseFromServer);
strReader = new StringReader(responseFromServer);
}
// Get new stream from webrequest
Stream commandStream = WebHelper.GetWebResponseStream(epoUrl, epoUsername, epoPassword);
StreamReader commandReader = new StreamReader(commandStream);
using (StreamWriter outfile = new StreamWriter(commandHelpPath, true, Encoding.UTF8))
{
while (!strReader.Peek().Equals(-1))
{
string streamLine = strReader.ReadLine();
string[] words = streamLine.Split(' ');
// Check if first string contains a period that's not at the end
if ((words[0].Contains(".")) & !(words[0].EndsWith(".")))
{
StreamReader helpReader = WebHelper.GetWebResponse(epoUrl + "?command=" + words[0], epoUsername, epoPassword);
string helpResponseFromServer = helpReader.ReadToEnd();
outfile.Write(helpResponseFromServer);
outfile.WriteLine("==============================");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Main exception: " + ex.Message);
}
finally
{
// Close streams
//coreReader.Close();
//commandReader.Close();
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
And the GetWebResponseStream method:
public static Stream GetWebResponseStream(string url, string username, string password)
{
Stream dataStream = null;
try
{
// Set the credentials.
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Basic", new System.Net.NetworkCredential(username, password));
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
// Create a request for the URL.
WebRequest request = WebRequest.Create(url);
request.Credentials = credentialCache;
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
return dataStream;
}
catch (Exception ex)
{
Console.WriteLine("GetWebResponse threw an exception: " + ex.Message);
return dataStream;
}
}
Thanks to Gildor for the suggestion that put me on the right track. I had the response, I just needed to copy that string into a StringReader. This automatically resets the cursor so you can read from the top! I updated the code to show the new fix. Thanks to everyone for the suggestions. John

How To Use HttpWebRequest/Response To Download A Binary (.exe) File From A Web Server?

I am writing a program that needs to download an .exe file from a website and then save it to the hard drive. The .exe is stored on my site and it's url is as follows (it's not the real uri just one I made up for the purpose of this question):
http://www.mysite.com/calc.exe
After many web searches and fumbling through examples here is the code I have come up with so far:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(http://www.mysite.com/calc.exe);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string s = streamReader.ReadToEnd();
As you can see I am using the StreamReader class to read the data. After calling ReadToEnd does the stream reader contain the (binary) content of my .exe? Can I just write the content of the StreamReader to a file (named calc.exe) and I will have succesfully downloaded the .exe?
I am wondering why StreamReader ReadToEnd returns a string. In my case would this string be the binary content of calc.exe?
WebClient is the best method to download file. But you can use the following method to download a file asynchronously from web server.
private static void DownloadCurrent()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("[url to download]");
webRequest.Method = "GET";
webRequest.Timeout = 3000;
webRequest.BeginGetResponse(new AsyncCallback(PlayResponeAsync), webRequest);
}
private static void PlayResponeAsync(IAsyncResult asyncResult)
{
long total = 0;
long received = 0;
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
try
{
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
{
byte[] buffer = new byte[1024];
FileStream fileStream = File.OpenWrite("[file name to write]");
using (Stream input = webResponse.GetResponseStream())
{
total = input.Length;
int size = input.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
received += size;
size = input.Read(buffer, 0, buffer.Length);
}
}
fileStream.Flush();
fileStream.Close();
}
}
catch (Exception ex)
{
}
}
There is a similar thread here - how to download the file using httpwebrequest
StreamReader is a text reader implementation i.e. it should be used to read text data and not binary data. In your case, you should be directly using the underlying response stream.
For downloading file, the simplest way would be to use WebClient.DownloadFile method.
This should directly save the file on your hard disk.
using System.Net;
using (WebClient webClient = new WebClient ())
{
webClient.DownloadFile("http://www.mysite.com/calc.exe", "calc.exe");
}
Instead of using StreamReader, you should really call Read() method of your Stream object. That will ask you for a byte[] buffer to be fill with read data, which you can then write to disk using StreamWriter or FileStream.
I'm probably a little bit late but I had the same problem with files being always 0kb big if not running in Debug mode.. This might be a relatively simple answer but disabling "DEBUG-Constants" under Properties solved it for me.
I have created a class with events so you can track download progress:
using System;
using System.IO;
using System.Net;
using System.Net.Mime;
//event examples: https://www.tutorialsteacher.com/csharp/csharp-event
public class HttpWebRequestDownload
{
private long _totalBytesLength = 0;
private long _transferredBytes = 0;
private int _transferredPercents => (int)((100 * _transferredBytes) / _totalBytesLength);
private string _defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public string downloadedFilePath = String.Empty;
public HttpWebRequestDownload(){
//Windows 7 fix
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
}
public void DownloadFile(string url, string destinationDirectory = default)
{
string filename = "";
if (destinationDirectory == default)
destinationDirectory = _defaultDirectory;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Cache-Control", "no-store");
request.Headers.Add("Cache-Control", "max-age=1");
request.Headers.Add("Cache-Control", "s-maxage=1");
request.Headers.Add("Pragma", "no-cache");
request.Headers.Add("Expires", "-1");
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result)
{
_totalBytesLength = response.ContentLength;
string path = response.Headers["Content-Disposition"];
if (string.IsNullOrWhiteSpace(path))
{
var uri = new Uri(url);
filename = Path.GetFileName(uri.LocalPath);
}
else
{
ContentDisposition contentDisposition = new ContentDisposition(path);
filename = contentDisposition.FileName;
}
using (Stream responseStream = response.GetResponseStream())
using (FileStream fileStream = File.Create(System.IO.Path.Combine(destinationDirectory, filename)))
{
byte[] buffer = new byte[1024*1024]; // 1MB buffer
ProgressEventArgs eventArgs = new ProgressEventArgs(_totalBytesLength);
int size = responseStream.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fileStream.Write(buffer, 0, size);
_transferredBytes += size;
size = responseStream.Read(buffer, 0, buffer.Length);
eventArgs.UpdateData(_transferredBytes, _transferredPercents);
OnDownloadProgressChanged(eventArgs);
}
}
}
downloadedFilePath = Path.Combine(destinationDirectory, filename);
OnDownloadFileCompleted(EventArgs.Empty);
}
catch (Exception e)
{
OnError($"{e.Message} => {e?.InnerException?.Message}");
}
}
//events
public event EventHandler<ProgressEventArgs> DownloadProgressChanged;
public event EventHandler DownloadFileCompleted;
public event EventHandler<string> Error;
public class ProgressEventArgs : EventArgs
{
public long TransferredBytes { get; set; }
public int TransferredPercents { get; set; }
public long TotalBytesLength { get; set; }
public ProgressEventArgs(long transferredBytes, int transferredPercents, long totalBytesLength)
{
TransferredBytes = transferredBytes;
TransferredPercents = transferredPercents;
TotalBytesLength = totalBytesLength;
}
public ProgressEventArgs(long totalBytesLength)
{
this.TotalBytesLength = totalBytesLength;
}
public void UpdateData(long transferredBytes, int transferredPercents)
{
TransferredBytes = transferredBytes;
TransferredPercents = transferredPercents;
}
}
protected virtual void OnDownloadProgressChanged(ProgressEventArgs e)
{
DownloadProgressChanged?.Invoke(this, e);
}
protected virtual void OnDownloadFileCompleted(EventArgs e)
{
DownloadFileCompleted?.Invoke(this, e);
}
protected virtual void OnError(string errorMessage)
{
Error?.Invoke(this, errorMessage);
}
}
Here is testing example:
static void Main(string[] args)
{
HttpWebRequestDownload hDownload = new HttpWebRequestDownload();
string downloadUrl = "http://speedtest.tele2.net/10MB.zip";
hDownload.DownloadProgressChanged += HDownloadOnDownloadProgressChanged;
hDownload.DownloadFileCompleted += delegate(object o, EventArgs args)
{
Debug.WriteLine("Download finished and saved to: "+hDownload.downloadedFilePath);
};
hDownload.Error += delegate(object o, string errMessage) { Debug.WriteLine("Error has occured !! => "+errMessage); };
hDownload.DownloadFile(downloadUrl);
}
private void HDownloadOnDownloadProgressChanged(object sender, HttpWebRequestDownload.ProgressEventArgs e)
{
Debug.WriteLine("progress: "+e.TransferredBytes+" => "+e.TransferredPercents);
}

How can I send multiple get requests to a URL in a C# console app?

I want to be able to send multiple GetRequest to a URL at the same time, and have it loop automatically. Can anyone give me the C# console coding?
This is the code:
using System;
using System.Net;
using System.IO;
namespace MakeAGETRequest_charp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string sURL;
sURL = "EXAMPLE.COM";
int things = 5;
while (things > 0)
{
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "1";
int i = 0;
i++;
sLine = objReader.ReadLine();
if (things > 0)
Console.WriteLine("{0}:{1}", i, sLine);
}
Console.ReadLine();
}
}
}
JK presents a synchronous version. The second URL won't be retrieved until a response is received from the first URL request.
Here's an asynchronous version:
List<Uri> uris = new List<Uri>();
uris.Add(new Uri("http://example.com"));
uris.Add(new Uri("http://example2.com"));
foreach(Uri u in uris)
{
var client = new WebClient();
client.DownloadDataCompleted += OnDownloadCompleted;
client.DownloadDataAsync(u); // this makes a GET request
}
...
void OnDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
// do stuff here. check e for completion, exceptions, etc.
}
See DownloadDataAsync documentation
List<Uri> uris = new List<Uri>();
uris.Add(new Uri("http://example.com"));
uris.Add(new Uri("http://example2.com"));
foreach(Uri u in uris)
{
WebRequest request = HttpWebRequest.Create(u);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
}

FileSystemWatcher for FTP

How can I implement a FileSystemWatcher for an FTP location (in C#). The idea is whenever anything gets added in the FTP location I wish to copy it to my local machine. Any ideas will be helpful.
This is a follow up of my previous question Selective FTP download using .NET.
You're going to have to implement a polling solution, where you keep asking for the directory content periodically. Compare this to a cached list from the previous call and determine what happened that way.
There's nothing in the FTP protocol that will help you with this unfortunately.
You cannot use the FileSystemWatcher or any other way, because the FTP protocol does not have any API to notify a client about changes in the remote directory.
All you can do is to periodically iterate the remote tree and find changes.
It's actually rather easy to implement, if you use an FTP client library that supports recursive listing of a remote tree. Unfortunately, the built-in .NET FTP client, the FtpWebRequest does not. But for example with WinSCP .NET assembly, you can use the Session.EnumerateRemoteFiles method.
See the article Watching for changes in SFTP/FTP server:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "password",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
List<string> prevFiles = null;
while (true)
{
// Collect file list
List<string> files =
session.EnumerateRemoteFiles(
"/remote/path", "*.*", EnumerationOptions.AllDirectories)
.Select(fileInfo => fileInfo.FullName)
.ToList();
if (prevFiles == null)
{
// In the first round, just print number of files found
Console.WriteLine("Found {0} files", files.Count);
}
else
{
// Then look for differences against the previous list
IEnumerable<string> added = files.Except(prevFiles);
if (added.Any())
{
Console.WriteLine("Added files:");
foreach (string path in added)
{
Console.WriteLine(path);
}
}
IEnumerable<string> removed = prevFiles.Except(files);
if (removed.Any())
{
Console.WriteLine("Removed files:");
foreach (string path in removed)
{
Console.WriteLine(path);
}
}
}
prevFiles = files;
Console.WriteLine("Sleeping 10s...");
Thread.Sleep(10000);
}
}
(I'm the author of WinSCP)
Though, if you actually want to just download the changes, it's a way easier. Just use the Session.SynchronizeDirectories in the loop.
session.SynchronizeDirectories(
SynchronizationMode.Local, "/remote/path", #"C:\local\path", true).Check();
See the article Keep local directory up to date (download changed files from remote SFTP/FTP server).
If you do not want to use a 3rd party library, you have to do with limitations of the FtpWebRequest. For an example how to recursively list a remote directory tree with the FtpWebRequest, see my answer to C# Download all files and subdirectories through FTP.
The FileSystemWatcher class works by registering for events with the host Windows operating system. As such, it is limited to working on local paths and UNC paths to directories hosted on Windows systems. The MSDN documentation on FileSystemWatcher explains the paths which you can use and some of the potential problems with using the class.
If you are looking to be alerted to changes on an FTP site, you will have to use a polling mechanism to ask for the current status of files or folders you are interested in monitoring. You will be able to see when files are added and removed by comparing snapshots of the FTP site for changes and raising similar events when you detect changes. Unfortunately you wont be able to detect rename events, but other changes should be simple to monitor this way.
Write a simple service to create FileSystemWatcher, pointing at your ftp location.
Then when a file is uploaded or modified, an event will be fired in your service, which you can then use to copy the file to your local machine.
File.Copy etc.
Hav a look at: this blog
You can monitor the FTP location by following method:
public class FtpFileSystemWatcher
{
public bool IsRunning
{
get;
private set;
}
public string FtpUserName
{
get;
set;
}
public string FtpPassword
{
get;
set;
}
public string FtpLocationToWatch
{
get;
set;
}
public string DownloadTo
{
get;
set;
}
public bool KeepOrignal
{
get;
set;
}
public bool OverwriteExisting
{
get;
set;
}
public int RecheckIntervalInSeconds
{
get;
set;
}
private bool DownloadInprogress
{
get;
set;
}
private System.Timers.Timer JobProcessor;
public FtpFileSystemWatcher(string FtpLocationToWatch = "", string DownloadTo = "", int RecheckIntervalInSeconds = 1, string UserName = "", string Password = "", bool KeepOrignal = false, bool OverwriteExisting = false)
{
this.FtpUserName = UserName;
this.FtpPassword = Password;
this.FtpLocationToWatch = FtpLocationToWatch;
this.DownloadTo = DownloadTo;
this.KeepOrignal = KeepOrignal;
this.RecheckIntervalInSeconds = RecheckIntervalInSeconds;
this.OverwriteExisting = OverwriteExisting;
if (this.RecheckIntervalInSeconds < 1) this.RecheckIntervalInSeconds = 1;
}
public void StartDownloading()
{
JobProcessor = new Timer(this.RecheckIntervalInSeconds * 1000);
JobProcessor.AutoReset = false;
JobProcessor.Enabled = false;
JobProcessor.Elapsed += (sender, e) =>
{
try
{
this.IsRunning = true;
string[] FilesList = GetFilesList(this.FtpLocationToWatch, this.FtpUserName, this.FtpPassword);
if (FilesList == null || FilesList.Length < 1)
{
return;
}
foreach (string FileName in FilesList)
{
if (!string.IsNullOrWhiteSpace(FileName))
{
DownloadFile(this.FtpLocationToWatch, this.DownloadTo, FileName.Trim(), this.FtpUserName, this.FtpPassword, this.OverwriteExisting);
if (!this.KeepOrignal)
{
DeleteFile(Path.Combine(this.FtpLocationToWatch, FileName.Trim()), this.FtpUserName, this.FtpPassword);
}
}
}
this.IsRunning = false;
JobProcessor.Enabled = true;
}
catch (Exception exp)
{
this.IsRunning = false;
JobProcessor.Enabled = true;
Console.WriteLine(exp.Message);
}
};
JobProcessor.Start();
}
public void StopDownloading()
{
try
{
this.JobProcessor.Dispose();
this.IsRunning = false;
}
catch { }
}
private void DeleteFile(string FtpFilePath, string UserName, string Password)
{
FtpWebRequest FtpRequest;
FtpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpFilePath));
FtpRequest.UseBinary = true;
FtpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
FtpRequest.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse response = (FtpWebResponse)FtpRequest.GetResponse();
response.Close();
}
private void DownloadFile(string FtpLocation, string FileSystemLocation, string FileName, string UserName, string Password, bool OverwriteExisting)
{
try
{
const int BufferSize = 2048;
byte[] Buffer = new byte[BufferSize];
FtpWebRequest Request;
FtpWebResponse Response;
if (File.Exists(Path.Combine(FileSystemLocation, FileName)))
{
if (OverwriteExisting)
{
File.Delete(Path.Combine(FileSystemLocation, FileName));
}
else
{
Console.WriteLine(string.Format("File {0} already exist.", FileName));
return;
}
}
Request = (FtpWebRequest)FtpWebRequest.Create(new Uri(Path.Combine(FtpLocation, FileName)));
Request.Credentials = new NetworkCredential(UserName, Password);
Request.Proxy = null;
Request.Method = WebRequestMethods.Ftp.DownloadFile;
Request.UseBinary = true;
Response = (FtpWebResponse)Request.GetResponse();
using (Stream s = Response.GetResponseStream())
{
using (FileStream fs = new FileStream(Path.Combine(FileSystemLocation, FileName), FileMode.CreateNew, FileAccess.ReadWrite))
{
while (s.Read(Buffer, 0, BufferSize) != -1)
{
fs.Write(Buffer, 0, BufferSize);
}
}
}
}
catch { }
}
private string[] GetFilesList(string FtpFolderPath, string UserName, string Password)
{
try
{
FtpWebRequest Request;
FtpWebResponse Response;
Request = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpFolderPath));
Request.Credentials = new NetworkCredential(UserName, Password);
Request.Proxy = null;
Request.Method = WebRequestMethods.Ftp.ListDirectory;
Request.UseBinary = true;
Response = (FtpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(Response.GetResponseStream());
string Data = reader.ReadToEnd();
return Data.Split('\n');
}
catch
{
return null;
}
}
}
The way I handle this is to upload a one element byte array, named ".ftpComplete". The FileSystemWatcher only watches for ".ftpComplete" files, and strips that off the end to know the actual file uploaded. Since the".ftpComplete" file is only 1 byte, it uploads about as fast as it is created on the FTP server, so it can be deleted once you do whatever you need to with the main uploaded file
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
FTPAddress + "/" + Path.GetFileName(filePath) + ".ftpComplete");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
byte[] buffer = new byte[1];
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
You could use a Robo-FTP script to monitor the FTP site for changes. Here is a link to a sample script that sends an email whenever a change is detected: http://kb.robo-ftp.com/script_library/show/40
I looked at the previous question that you linked. I think you should be able to modify the Robo-FTP sample and use the SETLEFT command with the /split option to make it parse the folder name and ISO file number of the changed file and then move the file to the proper location.

Categories