-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebDav.cs
126 lines (113 loc) · 4.42 KB
/
WebDav.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.IO;
using System.Net;
using System.Text;
using WebDAVSharp.Server;
using WebDAVSharp.Server.Stores.DiskStore;
namespace WebDAVTests
{
/// <summary>
/// The basics for talking to the WebDAV server
/// </summary>
internal static class WebDav
{
/// <summary>
/// Starts the WebDAV server.
/// </summary>
/// <returns>The <see cref="WebDavServer"/></returns>
internal static WebDavServer StartWebDavServer()
{
var server = new WebDavServer(new WebDavDiskStore(WebDavConfig.WebDavLocalPath));
server.Start(WebDavConfig.WebDavUri.ToString());
return server;
}
/// <summary>
/// Get the credentials of the user
/// </summary>
/// <param name="requestUri">The request URI</param>
/// <returns>
/// The credentials as an instance of <see cref="CredentialCache" />
/// </returns>
internal static CredentialCache WebDavCredentialCache(string requestUri)
{
var myCredentialCache = new CredentialCache
{
{
new Uri(requestUri),
"Negotiate",
new NetworkCredential(WebDavConfig.UserName, WebDavConfig.Password, WebDavConfig.Domain)
}
};
return myCredentialCache;
}
/// <summary>
/// Creates a basic WebDav Request
/// </summary>
/// <param name="requestUri">The requested URI as an <see cref="Uri" /></param>
/// <param name="method">The <see cref="string" /> defining the WebDAV method to be used</param>
/// <returns>
/// The basic WebDav Request as an <see cref="HttpWebRequest" />
/// </returns>
internal static HttpWebRequest WebDavRequestBase(string requestUri, string method)
{
// Create the HttpWebRequest object.
var request = (HttpWebRequest)WebRequest.Create(requestUri);
request.UserAgent = "WebDAV-UnitTestProject/1.0.0";
// Add the network credentials to the request.
request.UseDefaultCredentials = true;
// Specify the WebDAV method.
request.Method = method;
return request;
}
/// <summary>
/// Creates the response of the given <see cref="HttpWebRequest" />
/// </summary>
/// <param name="request">The <see cref="HttpWebRequest" /></param>
/// <returns>
/// The <see cref="WebResponse" /> of the <see cref="HttpWebRequest" />
/// </returns>
internal static WebResponse WebDavResponse(HttpWebRequest request)
{
try
{
// Send the WebDav method request, get the
// method response from the server.
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
if (stream != null)
{
var readStream = new StreamReader(stream, encode);
var read = new Char[256];
String str = "";
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
str += new String(read, 0, count);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the Stream.
readStream.Close();
}
// Close the HttpWebResponse object.
response.Close();
return response;
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
return ex.Response;
}
catch (Exception ex)
{
// Catch any exceptions. Any error codes from the WebDAV
// method request on the server will be caught here, also.
Console.WriteLine(ex.Message);
return null;
}
}
}
}