C# - .NET Framework 4.5.2 이하의 버전에서 HttpWebRequest로 https 호출 시 "System.Net.WebException" 예외 발생
아래의 코드를 .NET 4.5.2 이하의 버전에서 실행하면,
using System;
internal class Program
{
static void Main(string[] args)
{
string target = "https://msdl.microsoft.com/download/symbols/ntdll.pdb/09f4753312350aebf684ab6e5dff76a61/ntdll.pdb";
System.Net.HttpWebRequest req = System.Net.WebRequest.Create(target) as System.Net.HttpWebRequest;
req.Method = "HEAD";
using (System.Net.HttpWebResponse resp = req.GetResponse() as System.Net.HttpWebResponse)
{
Console.WriteLine(resp.ResponseUri);
}
}
}
이런 오류가 발생합니다.
Unhandled Exception: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest, Boolean renegotiation)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at Program.Main(String[] args)
가장 쉽게 해결하는 방법은 .NET Framework 4.6+ 또는 .NET Core로 업그레이드하는 것입니다.
문제의 원인은 아래의 글에 설명한 내용과 완전히 같습니다.
C# - WebClient로 https 호출 시 "The request was aborted: Could not create SSL/TLS secure channel" 예외 발생
; https://www.sysnet.pe.kr/2/0/13233
따라서 도입부에 이런 코드를 추가하면 문제가 해결됩니다.
using System;
internal class Program
{
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol |= (SecurityProtocolType)3072;
// 또는,
// ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
string target = "https://msdl.microsoft.com/download/symbols/ntdll.pdb/09f4753312350aebf684ab6e5dff76a61/ntdll.pdb";
System.Net.HttpWebRequest req = System.Net.WebRequest.Create(target) as System.Net.HttpWebRequest;
req.Method = "HEAD";
using (System.Net.HttpWebResponse resp = req.GetResponse() as System.Net.HttpWebResponse)
{
Console.WriteLine(resp.ResponseUri);
}
}
}
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]