ICRS-NBKI-Request

Форк
0
/
TestTLS.cs 
74 строки · 2.8 Кб
1
#region License
2
//------------------------------------------------------------------------------
3
// Copyright (c) Dmitrii Evdokimov
4
// Source https://github.com/diev/
5
// 
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//------------------------------------------------------------------------------
16
#endregion
17

18
using System;
19
using System.IO;
20
using System.Net;
21
using System.Security.Cryptography.X509Certificates;
22

23
namespace ICRS_NBKI_Request
24
{
25
    /// <summary>
26
    /// Тест двусторонней аутентификации TLS
27
    /// </summary>
28
    public static class TestTLS
29
    {
30
        /// <summary>
31
        /// Тест получения страницы при переходе на двустороннюю аутентификацию TLS
32
        /// </summary>
33
        public static void Run(X509Certificate certificate)
34
        {
35
            string uri = "https://reports.demo.nbki.ru/";
36
            string result = Request(uri, certificate);
37

38
            Console.WriteLine(result.Length > 200 ? result.Substring(0, 200) : result);
39
        }
40

41
        /// <summary>
42
        /// Запрос страницы для теста.
43
        /// </summary>
44
        /// <param name="uri">Адрес страницы.</param>
45
        /// <returns>Текст страницы.</returns>
46
        private static string Request(string uri, X509Certificate certificate)
47
        {
48
            var request = (HttpWebRequest)WebRequest.Create(uri);
49
            request.ClientCertificates.Add(certificate);
50

51
            return Response(request);
52
        }
53

54
        /// <summary>
55
        /// Получение страницы для теста.
56
        /// </summary>
57
        /// <param name="request">Запрос страницы.</param>
58
        /// <returns>Текст страницы.</returns>
59
        private static string Response(HttpWebRequest request)
60
        {
61
            var response = (HttpWebResponse)request.GetResponse();
62
            if (response.StatusCode != HttpStatusCode.OK)
63
            {
64
                throw new InvalidOperationException($"Unexpected behavior! Status code: {response.StatusCode}.");
65
            }
66

67
            using (var streamReader = new StreamReader(response.GetResponseStream()
68
                ?? throw new InvalidOperationException("Response stream is null.")))
69
            {
70
                return streamReader.ReadToEnd();
71
            }
72
        }
73
    }
74
}
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.