我有两个使用面向 .Net 框架 4.0 的System.Web.Http.SelfHost的自托管 .Net 应用程序,两个应用程序都通过 https 进行通信。
第一个应用程序使用端口:3287,第二个应用程序使用端口:3286
当我使用HttpWebRequest类从第一个应用程序调用第二个应用程序时,我得到了这个异常
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:49543
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
这是我的 C# 代码
HttpWebRequest request = WebRequest.Create("https://localhost.company.com:3286/"+ EndPoint) as HttpWebRequest;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.AllowAutoRedirect = false;
request.Expect = "application/json";
request.GetRequestStream().Write(data, 0, data.Length);
HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string s = streamReader.ReadToEnd();
return s;
}
因为我正在调用端口 3286,但在异常日志中端口号更改为 49543。
如果我通过邮递员调用我的应用程序,我可以得到它的响应。
证书 localhost.company.com 映射到 127.0.0.1,我也映射到了主机文件中。