Creat Web Request
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;
}
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"
Judge web exist or not
private static void EnsureWebResourceExists(string url, ICredentials creds)
{
WebRequest wr = GetWebRequest(url, "HEAD", creds);
wr.GetResponse();
}
异常捕捉到 404: 代表页面不存在, 401: 无法认证private static void EnsureWebResourceExists(string url, ICredentials creds)
{
WebRequest wr = GetWebRequest(url, "HEAD", creds);
wr.GetResponse();
}
创建Folder: "MKCOL "
Create Web Folder
private static void CreateWebFolder(string url, ICredentials creds)
{
try
{
EnsureWebResourceExists(url, creds);
return;
}
catch
{
}
WebRequest wr = GetWebRequest(url, "MKCOL", creds);
wr.GetResponse();
}
private static void CreateWebFolder(string url, ICredentials creds)
{
try
{
EnsureWebResourceExists(url, creds);
return;
}
catch
{
}
WebRequest wr = GetWebRequest(url, "MKCOL", creds);
wr.GetResponse();
}
上传文件: "PUT"
Upload File
private static void UploadFile(string url, string localFile, ICredentials creds)
{
WebClient wc = new WebClient();
wc.Credentials = creds;
wc.UploadFile(url, "PUT", localFile);
}
private static void UploadFile(string url, string localFile, ICredentials creds)
{
WebClient wc = new WebClient();
wc.Credentials = creds;
wc.UploadFile(url, "PUT", localFile);
}
判断页面是否为一个Folder: "PROPFIND"
Judge a Web Resource is Folder or not
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>"); ;
}
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>"); ;
}