ICRS-NBKI-Request
/
TestTLS.cs
74 строки · 2.8 Кб
1#region License2//------------------------------------------------------------------------------
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#endregion17
18using System;19using System.IO;20using System.Net;21using System.Security.Cryptography.X509Certificates;22
23namespace ICRS_NBKI_Request24{
25/// <summary>26/// Тест двусторонней аутентификации TLS27/// </summary>28public static class TestTLS29{30/// <summary>31/// Тест получения страницы при переходе на двустороннюю аутентификацию TLS32/// </summary>33public static void Run(X509Certificate certificate)34{35string uri = "https://reports.demo.nbki.ru/";36string result = Request(uri, certificate);37
38Console.WriteLine(result.Length > 200 ? result.Substring(0, 200) : result);39}40
41/// <summary>42/// Запрос страницы для теста.43/// </summary>44/// <param name="uri">Адрес страницы.</param>45/// <returns>Текст страницы.</returns>46private static string Request(string uri, X509Certificate certificate)47{48var request = (HttpWebRequest)WebRequest.Create(uri);49request.ClientCertificates.Add(certificate);50
51return Response(request);52}53
54/// <summary>55/// Получение страницы для теста.56/// </summary>57/// <param name="request">Запрос страницы.</param>58/// <returns>Текст страницы.</returns>59private static string Response(HttpWebRequest request)60{61var response = (HttpWebResponse)request.GetResponse();62if (response.StatusCode != HttpStatusCode.OK)63{64throw new InvalidOperationException($"Unexpected behavior! Status code: {response.StatusCode}.");65}66
67using (var streamReader = new StreamReader(response.GetResponseStream()68?? throw new InvalidOperationException("Response stream is null.")))69{70return streamReader.ReadToEnd();71}72}73}74}
75