Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active March 9, 2023 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnLBevan/c1b728a9ace26c86ad7037e540000edb to your computer and use it in GitHub Desktop.
Save JohnLBevan/c1b728a9ace26c86ad7037e540000edb to your computer and use it in GitHub Desktop.
A basic c# client for testing webdav endpoints
void Main()
{
var usr = @"myDomain\myUser";
var pwd = Util.GetPassword(usr); //linqpad util library
var uri = "https://webdavendpoint.example.com/somefolder/";
var fn = @"c:\temp\filetouploadTestData.txt";
var rn = "remoteFilenameTestData.txt";
var wd = new BasicWebDavClient(usr, pwd, uri);
wd.Put(fn, rn);
wd.PropFind();
wd.Get(rn, fn + "_downloaded.txt");
}
class BasicWebDavClient
{
private readonly string username;
private readonly string password;
private readonly string uri;
public BasicWebDavClient(string username, string password, string uri)
{
this.username = username;
this.password = password;
this.uri = uri;
}
private HttpWebRequest CreateRequest(string requestUri, string method)
{
var req = (HttpWebRequest)WebRequest.Create(requestUri);
req.Credentials = new NetworkCredential(username, password);
req.PreAuthenticate = true;
req.Method = method;
return req;
}
public void Put(string fn, string name)
{
var content = File.ReadAllBytes(fn);
var target = uri + name;
var req = CreateRequest(target, "PUT");
req.Headers.Add("Overwrite", "T"); // T,F :S - https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/aa142944(v=exchg.65)
req.ContentLength = content.Length;
//req.SendChunked = true;
using (var rs = req.GetRequestStream())
{
rs.Write(content, 0, content.Length);
}
using (var resp = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(@"PUT Response: {0}", resp.StatusDescription); // Created if successful, No Content when file already exists?
}
}
public void Get(string name, string fn)
{
var source = uri + name;
var req = CreateRequest(source, "GET");
req.Headers.Add(@"Translate", "F"); // i.e. F = return the raw file from the server; don't try to process it serverside first: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-wdvse/501879f9-3875-4d7a-ab88-3cecab440034. Note: Requires SOURCE permission
using (var resp = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(@"GET Response: {0}", resp.StatusDescription);
using (var rs = resp.GetResponseStream())
{
using (var ms = new MemoryStream())
{
rs.CopyTo(ms);
File.WriteAllBytes(fn, ms.ToArray());
}
}
}
}
// as yet untested
public void Delete(string name)
{
var target = uri + name;
var req = CreateRequest(target, "DELETE");
using (var resp = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(@"DELETE Response: {0}", resp.StatusDescription);
}
}
public void PropFind()
{
var req = CreateRequest(uri, "PROPFIND");
using (var resp = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(@"PROPFIND Response: {0}", resp.StatusDescription);
var enc = Encoding.GetEncoding(resp.CharacterSet);
using (var responseStream = resp.GetResponseStream())
{
using (var sr = new StreamReader(responseStream, enc))
{
Console.WriteLine("Content: ");
var xml = new XmlDocument();
xml.LoadXml(sr.ReadToEnd());
foreach (XmlNode node in xml.SelectNodes("//*[local-name()='href']"))
{
Console.WriteLine(" - " + node.InnerText);
}
}
}
}
}
// other webdav methods include MOVE, COPY, MKCOL, etc
// list of some of these here: https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/aa142917(v=exchg.65)
}
@JohnLBevan
Copy link
Author

Additional Namespace Imports (F4):

  • System
  • System.IO
  • System.Net
  • System.Net.Security
  • System.Security.Cryptography.X509Certificates
  • System.Text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment