namespace myOwncloudProj
{
class Program
{
static void Main(string[] args)
{
try {
var client = new RestClient();
string cloudUsr = "user";
string cloudPw = "pw";
string pathToSave = @"D:\path\to\files\";
client.Authenticator = new HttpBasicAuthenticator(cloudUsr, cloudPw);
UploadToCloud(client, "tmp.docx", pathToSave);
DownloadToPC(client, "tmp.docx", pathToSave);
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
// Verzeichnis existiert nicht
catch (DirectoryNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException webExcp)
{
// If you reach this point, an exception has been caught.
Console.WriteLine("A WebException has been caught.");
// Write out the WebException message.
Console.WriteLine(webExcp.ToString());
// Get the WebException status code.
WebExceptionStatus status = webExcp.Status;
// If status is WebExceptionStatus.ProtocolError,
// there has been a protocol error and a WebResponse
// should exist. Display the protocol error.
if (status == WebExceptionStatus.ProtocolError)
{
Console.Write("The server returned protocol error ");
// Get HttpWebResponse so that you can check the HTTP status code.
HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
Console.WriteLine((int)httpResponse.StatusCode + " - "
+ httpResponse.StatusCode);
}
}
catch (Exception e_cld)
{
Console.WriteLine("An error occurred: '{0}'", e_cld);
}
Console.ReadLine();
}
//Upload file to owncloud server
public static void UploadToCloud(RestClient ocClient, string fileName, string ocLocal)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//Check if the file exists on PC
string localFile = ocLocal + fileName;
if (File.Exists(localFile) == true)
{
Console.WriteLine("local file exits on pc. \n");
//Check if the file exits on server *********
//Or upload it directlly
var requestUpLoad = new RestRequest("/", Method.POST);
requestUpLoad.AddHeader("Content-Type", "application/octet-stream");
byte[] dataToUpload = File.ReadAllBytes(localFile);
requestUpLoad.AddFile("tmpFile", dataToUpload, "tmp.docx");
Console.WriteLine("Size of file to upload: " + dataToUpload.Length.ToString() + "\n");
//ocClient.Execute(requestUpLoad);
var cloudResponse = ocClient.Execute(requestUpLoad);
Console.WriteLine("Resp: " + cloudResponse.ContentType + "\n");
Console.WriteLine("Content: " + cloudResponse.Content + "\n");
Console.WriteLine("Upload has been finished. \n");
}
else
{
Console.WriteLine("\n" + fileName + " is not found on pc. \n");
}
}
}
}