Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WebPutter.cs
1using System;
2using System.Collections.Generic;
3using System.Net.Http;
4using System.Net.Http.Headers;
5using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
10
12{
16 public class WebPutter : PutterBase
17 {
21 public WebPutter()
22 {
23 }
24
28 public override string[] UriSchemes => new string[] { "http", "https" };
29
36 public override bool CanPut(Uri Uri, out Grade Grade)
37 {
38 switch (Uri.Scheme.ToLower())
39 {
40 case "http":
41 case "https":
42 Grade = Grade.Ok;
43 return true;
44
45 default:
46 Grade = Grade.NotAtAll;
47 return false;
48 }
49 }
50
62 public override async Task<KeyValuePair<byte[], string>> PutAsync(Uri Uri, byte[] EncodedData, string ContentType,
63 X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs,
64 params KeyValuePair<string, string>[] Headers)
65 {
66 HttpClientHandler Handler = WebGetter.GetClientHandler(Certificate, RemoteCertificateValidator);
67 using (HttpClient HttpClient = new HttpClient(Handler, true)
68 {
69 Timeout = TimeSpan.FromMilliseconds(TimeoutMs)
70 })
71 {
72 using (HttpRequestMessage Request = new HttpRequestMessage()
73 {
74 RequestUri = Uri,
75 Method = HttpMethod.Put,
76 Content = new ByteArrayContent(EncodedData)
77 })
78 {
79 Request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType);
80 WebGetter.PrepareHeaders(Request, Headers, Handler);
81
82 HttpResponseMessage Response = await HttpClient.SendAsync(Request);
83
84 return await WebPoster.ProcessResponse(Response, Uri);
85 }
86 }
87 }
88
89 }
90}
Gets resources from the Web (i.e. using HTTP or HTTPS).
Definition: WebGetter.cs:20
static HttpClientHandler GetClientHandler()
Gets a HTTP Client handler
Definition: WebGetter.cs:107
Posts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPoster.cs:17
static async Task< KeyValuePair< byte[], string > > ProcessResponse(HttpResponseMessage Response, Uri Uri)
Decodes a response from the web. If the response is a success, the decoded response is returned....
Definition: WebPoster.cs:97
Abstract base class for putters.
Definition: PutterBase.cs:13
Puts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPutter.cs:17
override string[] UriSchemes
Supported URI schemes.
Definition: WebPutter.cs:28
override async Task< KeyValuePair< byte[], string > > PutAsync(Uri Uri, byte[] EncodedData, string ContentType, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Puts to a resource, using a Uniform Resource Identifier (or Locator).
Definition: WebPutter.cs:62
override bool CanPut(Uri Uri, out Grade Grade)
If the putter is able to put to a resource, given its URI.
Definition: WebPutter.cs:36
WebPutter()
Puts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPutter.cs:21
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.
Grade
Grade enumeration
Definition: Grade.cs:7