

private static WebRequest GetWebRequest(string url, string method, ICredentials creds)
{
Uri u = new Uri(url);
if ((Uri.UriSchemeHttp != u.Scheme) && (Uri.UriSchemeHttps != u.Scheme))
{
throw new ArgumentOutOfRangeException("url", url);
}
WebRequest wr = WebRequest.Create(u);
HttpWebRequest http = wr as HttpWebRequest;
http.AllowAutoRedirect = false;
http.Method = method;
http.Credentials = creds;
http.PreAuthenticate = true;
return http;
}
判断URL是否存在或者有无权限访问: "HEAD"


private static void EnsureWebResourceExists(string url, ICredentials creds)
{
WebRequest wr = GetWebRequest(url, "HEAD", creds);
wr.GetResponse();
}
创建Folder: "MKCOL "


private static void CreateWebFolder(string url, ICredentials creds)
{
try
{
EnsureWebResourceExists(url, creds);
return;
}
catch
{
}
WebRequest wr = GetWebRequest(url, "MKCOL", creds);
wr.GetResponse();
}
上传文件: "PUT"


private static void UploadFile(string url, string localFile, ICredentials creds)
{
WebClient wc = new WebClient();
wc.Credentials = creds;
wc.UploadFile(url, "PUT", localFile);
}
判断页面是否为一个Folder: "PROPFIND"


private static bool IsWebResourceAFolder(string url, ICredentials creds)
{
WebRequest wr = GetWebRequest(url, "PROPFIND", creds);
wr.Headers.Add("Depth:0");
wr.ContentLength = 0;
WebResponse resp = wr.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string s = sr.ReadToEnd();
//this is a hack - should probably parse the XML, but this is good enough for now
return s.Contains(@"<D:isFolder>t</D:isFolder>") || s.Contains("<a:iscollection b:dt=\"boolean\">1</a:iscollection>"); ;
}