### MyHouse V1.0.2502.0801 - *.[新增]适配新版接口。 ### SmartHouseAPI V1.0.2502.0801 - *.[新增]支持Docker部署,支持NAS。
108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyHouse.API
|
|
{
|
|
public class WebDav
|
|
{
|
|
private string _UserName = "";
|
|
private string _Password = "";
|
|
private string _WebDavUrl = "";
|
|
public WebDav(string _WebDavUrl,string _UserName,string _Password)
|
|
{
|
|
this._UserName = _UserName;
|
|
this._Password = _Password;
|
|
this._WebDavUrl = _WebDavUrl;
|
|
}
|
|
public int DelFile(string _WebFileUrl)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_WebDavUrl+"/"+_WebFileUrl);
|
|
req.Credentials = new NetworkCredential(_UserName, _Password); //验证密码
|
|
req.PreAuthenticate = true;
|
|
req.Method = "DELETE";
|
|
//req.AllowWriteStreamBuffering = true;
|
|
|
|
req.GetResponse();
|
|
return 1;
|
|
}
|
|
catch { return 0; }
|
|
}
|
|
public static Image LoadPic(string url)
|
|
{
|
|
Image image = null;
|
|
try
|
|
{
|
|
WebRequest webreq = WebRequest.Create(url);
|
|
WebResponse webres = webreq.GetResponse();
|
|
Stream stream = webres.GetResponseStream();
|
|
image = Image.FromStream(stream);
|
|
stream.Close();
|
|
}
|
|
catch { }
|
|
return image;
|
|
}
|
|
public int Upload(string _WebFileUrl, string _LocalFile)
|
|
{
|
|
try
|
|
{
|
|
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)WebRequest.Create(_WebDavUrl + "/" + _WebFileUrl); //Http和服务器交互
|
|
req.Credentials = new NetworkCredential(_UserName, _Password); //验证密码
|
|
req.PreAuthenticate = true;
|
|
req.Method = "PUT";//采用PUT方式
|
|
req.AllowWriteStreamBuffering = true;
|
|
|
|
Stream reqStream = req.GetRequestStream();
|
|
FileStream rdm = new FileStream(_LocalFile, FileMode.Open); //打开本地文件流
|
|
|
|
byte[] inData = new byte[4096];
|
|
int byteRead = rdm.Read(inData, 0, inData.Length); //二进制读文件
|
|
while (byteRead > 0)
|
|
{
|
|
reqStream.Write(inData, 0, byteRead); //响应流写入
|
|
byteRead = rdm.Read(inData, 0, inData.Length);
|
|
}
|
|
rdm.Close();
|
|
reqStream.Close();
|
|
|
|
req.GetResponse(); //提交
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return 0;
|
|
}
|
|
return 1; //正确返回
|
|
}
|
|
//----新建文件夾----
|
|
|
|
public int CreateDir(string Path)
|
|
{
|
|
try
|
|
{
|
|
// Create the HttpWebRequest object.
|
|
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)WebRequest.Create(_WebDavUrl + "/" + Path); //Http和服务器交互
|
|
req.Credentials = new NetworkCredential(_UserName, _Password); //验证密码
|
|
// Specify the method.
|
|
req.Method = "MKCOL";
|
|
|
|
HttpWebResponse objResponse = (System.Net.HttpWebResponse)req.GetResponse();
|
|
|
|
// Close the HttpWebResponse object.
|
|
objResponse.Close();
|
|
return 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|