代码之家  ›  专栏  ›  技术社区  ›  MarkKGreenway

来自ASP.NET的WebClient给出“远程主机强制关闭了现有连接”错误

  •  4
  • MarkKGreenway  · 技术社区  · 16 年前

    我想把电话列表贴到星号框里

    从一个控制台应用程序中,它可以工作:

     class Program
      {
        static void Main(string[] args)
        {
    
    
            Console.WriteLine( HttpPost());
            System.Threading.Thread.Sleep(10000);
        }
    
        public static string HttpPost()
        {
            var URI = @"http://sip.ligmarine.com/admin/config.php?quietmode=on&type=tool&display=printextensions";
            var Parameters = "display=printextensions&quietmode=on&type=tool&core=core&featurecodeadmin=featurecodeadmin&paging=paging";
            var retVal = "";
            WebClient wc = new WebClient();
    
            wc.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("maint:password")));
            wc.Headers.Add("referer", @"http://sip.ligmarine.com/admin/config.php?type=tool&display=printextensions");
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
            retVal = Convert.ToBase64String(Encoding.ASCII.GetBytes("maint:password"));
            //Console.Write("Resulting Request Headers: ");
            //Console.WriteLine(wc.Headers.ToString());
            byte[] byteArray = Encoding.ASCII.GetBytes(Parameters);
            //Console.WriteLine("Uploading to {0} ...", URI);
            // Upload the input string using the HTTP 1.0 POST method.
            byte[] responseArray = wc.UploadData(URI, "POST", byteArray);
           // Console.WriteLine("\nResponse received was {0}", );
    
            retVal = Encoding.ASCII.GetString(responseArray);
    
            return retVal;
        }
    }
    

    从我们的iis6托管的ASP.NET页面我得到

    远程主机强制关闭了现有连接 描述:当前网站执行期间发生未处理的异常
    请求。请查看堆栈跟踪以获取有关错误和
    它起源于代码。

     Exception Details: System.Net.Sockets.SocketException: An existing connection was      forcibly closed by the remote host
    
     Source Error:
    
     Line 37:         //Console.WriteLine("Uploading to {0} ...", URI);
     Line 38:         // Upload the input string using the HTTP 1.0 POST method.
     Line 39:         byte[] responseArray = wc.UploadData(URI, "POST", byteArray);
     Line 40:         // Console.WriteLine("\nResponse received was {0}", );
     Line 41: 
    

    httppost方法与页面加载完全相同:

    protected void Page_Load(object sender, EventArgs e)
    {
    
        var ret = HttpPost();
        Response.Write(ret);
    }
    
    3 回复  |  直到 8 年前
        1
  •  15
  •   joym8    9 年前

    我的情况非常相似,但解决方法不同。在我的Windows 10 dev machine+控制台应用程序上, WebClient.UploadData 到A https 地址工作得很好。但是,当相同的精确函数复制到ASP.NET MVC应用程序,并发布到不同的Web服务器(Windows 2008 R2)时,出现了以下异常:

    System.net.WebException:基础连接已关闭:一个 发送时发生意外错误。--->system.io.io异常: 无法从传输连接读取数据:现有 远程主机强制关闭了连接。---gt; system.net.sockets.socketException:现有连接为 远程主机强制关闭

    两个项目都使用.NET Framework 4.6.1

    通过使用呼叫解决 TLS1.2 . 在前面添加这个 UploadData :

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    Source

        2
  •  1
  •   MarkKGreenway    16 年前

    这是一个DNS问题…服务器正在解析到专用IP控制台应用程序正在解析为公用

        3
  •  1
  •   Tommy Egeberg    8 年前

    谢谢,你救了我一天。 对Azure AppService应用程序的请求正在引发“…连接已关闭“异常。

    设置 安全协议类型.tls12 解决了问题。

    也与

    _webClient.DownloadString(url);
    

    var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    var resp = webRequest.GetResponse();