forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
169 lines (141 loc) · 7.5 KB
/
Program.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using nanoFramework.Networking;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
#if HAS_WIFI
using System.Device.Wifi;
#endif
namespace HttpWebRequestSample
{
public class Program
{
#if HAS_WIFI
private static string MySsid = "ssid";
private static string MyPassword = "password";
#endif
public static void Main()
{
Debug.WriteLine("Waiting for network up, IP address and valid date & time...");
bool success;
CancellationTokenSource cs = new(60000);
#if HAS_WIFI
// if the device doesn't have the Wifi credentials stored
//success = WifiNetworkHelper.ConnectDhcp(MySsid, MyPassword, requiresDateTime: true, token: cs.Token);
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
// if the device has the Wifi credentials already stored, this call it's faster //
success = WifiNetworkHelper.Reconnect(requiresDateTime: true, token: cs.Token); //
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#else
success = NetworkHelper.SetupAndConnectNetwork(cs.Token, true);
#endif
if (!success)
{
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.Status}.");
if (NetworkHelper.HelperException != null)
{
Debug.WriteLine($"Exception: {NetworkHelper.HelperException}");
}
return;
}
// follow some test URLs and root CA certificates
/////////////////////////////////////////////////////////////////////////////////////
////add certificate in PEM format(as a string in the app)
//X509Certificate rootCACert = new X509Certificate(letsEncryptCACertificate);
////test URL for Let's encrypt certificate
//string url = "https://www.howsmyssl.com";
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
// add certificate from resources in CRT format
X509Certificate rootCACert = new X509Certificate(Resources.GetBytes(Resources.BinaryResources.DigiCertGlobalRootCA));
// test URL for DigiCert certificate
string url = "https://global-root-ca.chain-demos.digicert.com";
///////////////////////////////////////////////////////////////////////////////////
Debug.WriteLine($"Performing Http request to: {url}");
// perform the request as a HttpWebRequest
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
//////////////////////////////////////////////////////////////////////
// need to set the SSL protocol that the connection is going to use //
// *** this MANDATORY otherwise the authentication will fail *** //
//////////////////////////////////////////////////////////////////////
httpWebRequest.SslProtocols = System.Net.Security.SslProtocols.Tls12;
// if the request is to a secured server we need to make sure that we either:
// 1. provide the root CA certificate
// 2. the device has already stored a root CA bundle that will use when performing the authentication
httpWebRequest.HttpsAuthentCert = rootCACert;
int totalBytesRead = 0;
// get the response as a HttpWebResponse
// wrap the response object with a using statement to make sure that it's disposed
using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
// wrap the response stream on a using statement to make sure that it's disposed
using (var stream = httpWebResponse.GetResponseStream())
{
// read response in chunks of 1k
byte[] buffer = new byte[1024];
int bytesRead = 0;
Debug.WriteLine("Http response follows");
Debug.WriteLine(">>>>>>>>>>>>>");
do
{
bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
totalBytesRead += bytesRead;
Debug.Write(Encoding.UTF8.GetString(buffer, 0, bytesRead));
}
}
while (totalBytesRead < httpWebResponse.ContentLength);
}
Debug.WriteLine(">>>>>>>>>>>>>");
Debug.WriteLine("End of Http response");
Debug.WriteLine($"Read {totalBytesRead} bytes");
}
Thread.Sleep(Timeout.Infinite);
}
// Let’s Encrypt Authority X3 (IdenTrust cross-signed)
// from https://letsencrypt.org/certificates/
// X509 RSA key PEM format
private const string letsEncryptCACertificate =
@"-----BEGIN CERTIFICATE-----
MIIFjTCCA3WgAwIBAgIRANOxciY0IzLc9AUoUSrsnGowDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTYxMDA2MTU0MzU1
WhcNMjExMDA2MTU0MzU1WjBKMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg
RW5jcnlwdDEjMCEGA1UEAxMaTGV0J3MgRW5jcnlwdCBBdXRob3JpdHkgWDMwggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCc0wzwWuUuR7dyXTeDs2hjMOrX
NSYZJeG9vjXxcJIvt7hLQQWrqZ41CFjssSrEaIcLo+N15Obzp2JxunmBYB/XkZqf
89B4Z3HIaQ6Vkc/+5pnpYDxIzH7KTXcSJJ1HG1rrueweNwAcnKx7pwXqzkrrvUHl
Npi5y/1tPJZo3yMqQpAMhnRnyH+lmrhSYRQTP2XpgofL2/oOVvaGifOFP5eGr7Dc
Gu9rDZUWfcQroGWymQQ2dYBrrErzG5BJeC+ilk8qICUpBMZ0wNAxzY8xOJUWuqgz
uEPxsR/DMH+ieTETPS02+OP88jNquTkxxa/EjQ0dZBYzqvqEKbbUC8DYfcOTAgMB
AAGjggFnMIIBYzAOBgNVHQ8BAf8EBAMCAYYwEgYDVR0TAQH/BAgwBgEB/wIBADBU
BgNVHSAETTBLMAgGBmeBDAECATA/BgsrBgEEAYLfEwEBATAwMC4GCCsGAQUFBwIB
FiJodHRwOi8vY3BzLnJvb3QteDEubGV0c2VuY3J5cHQub3JnMB0GA1Ud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