来自Asp.net的WebClient给出"现有连接被远程主机强行关闭"错误

Mar*_*way 8 c# asp.net webclient

我想发布到我们的星号框来解析电话列表

从控制台应用程序这工作:

 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

远程主机强制关闭现有连接说明:执行当前Web
请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其
源自代码的位置的更多信息.

 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: 
Run Code Online (Sandbox Code Playgroud)

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

protected void Page_Load(object sender, EventArgs e)
{

    var ret = HttpPost();
    Response.Write(ret);
}
Run Code Online (Sandbox Code Playgroud)

joy*_*ym8 21

我有非常相似的情况但不同的解决方案.在我的Windows 10开发机+控制台应用程序上,WebClient.UploadData到一个https地址工作正常.但是当相同的功能被复制到ASP.NET MVC应用程序并发布到不同的Web服务器(Windows 2008 R2)时,它会给出以下异常:

System.Net.WebException:基础连接已关闭:发送时发生意外错误.---> System.IO.IOException:无法从传输连接读取数据:远程主机强制关闭现有连接.---> System.Net.Sockets.SocketException:远程主机强行关闭现有连接

这两个项目都使用.NET framework 4.6.1

通过拨打电话解决问题TLS1.2.在此之前添加UploadData:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

资源


Mar*_*way 2

这是一个 dns 问题...服务器正在解析为私有 ip 控制台应用程序正在解析为公共