Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WebPoster.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 WebPoster : PosterBase
17 {
21 public WebPoster()
22 {
23 }
24
28 public override string[] UriSchemes => new string[] { "http", "https" };
29
36 public override bool CanPost(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>> PostAsync(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.Post,
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 ProcessResponse(Response, Uri);
85 }
86 }
87 }
88
97 public static async Task<KeyValuePair<byte[], string>> ProcessResponse(HttpResponseMessage Response, Uri Uri)
98 {
99 if (!Response.IsSuccessStatusCode)
100 await WebGetter.ProcessResponse(Response, Uri);
101
102 byte[] Bin = await Response.Content.ReadAsByteArrayAsync();
103 string ContentType = Response.Content.Headers.ContentType?.ToString() ?? BinaryCodec.DefaultContentType;
104
105 return new KeyValuePair<byte[], string>(Bin, ContentType);
106 }
107
108 }
109}
const string DefaultContentType
text/plain
Definition: BinaryCodec.cs:24
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
static async Task< object > ProcessResponse(HttpResponseMessage Response, Uri Uri)
Decodes a response from the web. If the response is a success, the decoded response is returned....
Definition: WebGetter.cs:189
Abstract base class for posters.
Definition: PosterBase.cs:13
Posts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPoster.cs:17
WebPoster()
Posts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPoster.cs:21
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
override string[] UriSchemes
Supported URI schemes.
Definition: WebPoster.cs:28
override bool CanPost(Uri Uri, out Grade Grade)
If the poster is able to post to a resource, given its URI.
Definition: WebPoster.cs:36
override async Task< KeyValuePair< byte[], string > > PostAsync(Uri Uri, byte[] EncodedData, string ContentType, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Posts to a resource, using a Uniform Resource Identifier (or Locator).
Definition: WebPoster.cs:62
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.
Grade
Grade enumeration
Definition: Grade.cs:7