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;
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<ContentBinaryResponse> PostAsync(Uri Uri, byte[] EncodedData, string ContentType,
63 X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, int TimeoutMs,
64 params KeyValuePair<string, string>[] Headers)
65 {
66 HttpClientHandler Handler = WebGetter.GetClientHandler(Certificate, RemoteCertificateValidator, Uri);
67 using (HttpClient HttpClient = new HttpClient(Handler, true)
68 {
69 Timeout = TimeSpan.FromMilliseconds(TimeoutMs)
70 })
71 {
72 using (HttpRequestMessage Request = new HttpRequestMessage()
73 {
74 Method = HttpMethod.Post,
75 })
76 {
77 if (EncodedData?.Length > 0 || !string.IsNullOrEmpty(ContentType))
78 Request.Content = new ByteArrayContent(EncodedData);
79
80 Request.RequestUri = WebGetter.CheckUri(Uri, Request);
81
82 if (!string.IsNullOrEmpty(ContentType))
83 Request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType);
84
85 WebGetter.PrepareHeaders(Request, Headers, Handler);
86
87 HttpResponseMessage Response = await HttpClient.SendAsync(Request);
88
89 return await ProcessResponse(Response, Uri);
90 }
91 }
92 }
93
102 public static async Task<ContentBinaryResponse> ProcessResponse(HttpResponseMessage Response, Uri Uri)
103 {
104 if (!Response.IsSuccessStatusCode)
105 {
106 ContentResponse Temp = await WebGetter.ProcessResponse(Response, Uri);
107 return new ContentBinaryResponse(Temp.Error);
108 }
109
110 byte[] Bin = await Response.Content.ReadAsByteArrayAsync();
111
112 string ContentType = Response.Content.Headers.ContentType?.ToString()
113 ?? (Bin.Length == 0 ? string.Empty : BinaryCodec.DefaultContentType);
114
115 return new ContentBinaryResponse(ContentType, Bin);
116 }
117
118 }
119}
const string DefaultContentType
text/plain
Definition: BinaryCodec.cs:24
Contains information about a binary response to a content request.
Contains information about a response to a content request.
Exception Error
Error response.
Gets resources from the Web (i.e. using HTTP or HTTPS).
Definition: WebGetter.cs:25
static async Task< ContentResponse > 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:386
static void PrepareHeaders(HttpRequestMessage Request, KeyValuePair< string, string >[] Headers, HttpClientHandler Handler)
Prepares headers for a HTTP request.
Definition: WebGetter.cs:447
static HttpClientHandler GetClientHandler()
Gets a HTTP Client handler
Definition: WebGetter.cs:153
static Uri CheckUri(Uri Uri, HttpRequestMessage Request)
Checks the URI, if it is suitable for processing, or if it needs to be modified.
Definition: WebGetter.cs:260
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
override async Task< ContentBinaryResponse > PostAsync(Uri Uri, byte[] EncodedData, string ContentType, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Posts to a resource, using a Uniform Resource Identifier (or Locator).
Definition: WebPoster.cs:62
static async Task< ContentBinaryResponse > 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:102
WebPoster()
Posts to resources on the Web (i.e. using HTTP or HTTPS).
Definition: WebPoster.cs:21
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
Grade
Grade enumeration
Definition: Grade.cs:7