首页 > 解决方案 > 为什么在尝试连接到 localhost 时会出现错误?

问题描述

我正在开发一个需要连接到运行 express.js 的 node.js 服务器的项目,该服务器正在我的计算机上运行。我运行以下代码:

HttpWebRequest myRequest;
WebResponse myResponse;
StreamReader sr;
myRequest = (HttpWebRequest)WebRequest.Create("https://localhost:1337/Test.txt");
myRequest.Method = "GET";
myResponse = myRequest.GetResponse();
sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
sr.Close();
myResponse.Close();

并得到以下错误:

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message=The invocation of the constructor on type 'MyNamespace.MyClass' that matches the specified binding constraints threw an exception.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)

  This exception was originally thrown at this call stack:
    System.Net.Security.SslStream.StartReadFrame(byte[], int, System.Net.AsyncProtocolRequest)
    System.Net.Security.SslStream.PartialFrameCallback(System.Net.AsyncProtocolRequest)
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Net.Security.SslStream.ThrowIfExceptional()
    System.Net.Security.SslStream.InternalEndProcessAuthentication(System.Net.LazyAsyncResult)
    System.Net.Security.SslStream.EndProcessAuthentication(System.IAsyncResult)
    System.Net.Security.SslStream.EndAuthenticateAsClient(System.IAsyncResult)
    System.Net.Security.SslStream.AuthenticateAsClientAsync.AnonymousMethod__65_1(System.IAsyncResult)
    System.Threading.Tasks.TaskFactory<TResult>.FromAsyncCoreLogic(System.IAsyncResult, System.Func<System.IAsyncResult, TResult>, System.Action<System.IAsyncResult>, System.Threading.Tasks.Task<TResult>, bool)
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    ...
    [Call Stack Truncated]
Inner Exception 1:
WebException: The SSL connection could not be established, see inner exception. The handshake failed due to an unexpected packet format.

Inner Exception 2:
HttpRequestException: The SSL connection could not be established, see inner exception.

Inner Exception 3:
IOException: The handshake failed due to an unexpected packet format.

该代码旨在连接到服务器,读取在其上运行的测试文件并返回文件的内容。

我也试过关闭windows防火墙,但它什么也没做。

标签: c#httpwebrequest

解决方案


推荐阅读