WebClient from Asp.net gives "an existing connection was forcibly closed by the remote host" Error

I am trying to publish an asterisk in our window to parse the list of phones

from a console app this works:

 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;
    }
}

      

from our IIS6 Hosted ASP.NET page I get

an existing connection was forcibly closed by the remote host Description: An unhandled exception occurred during the execution of the current website request. Check out the stack trace for more information on the error and where it originated from the code.

 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: 

      

The HttpPost method is exactly identical to page load:

protected void Page_Load(object sender, EventArgs e)
{

    var ret = HttpPost();
    Response.Write(ret);
}

      

+7


a source to share


4 answers


It was a dns issue ... the server was solving the private ip console app to allow publishing



+2


a source


I had a very similar situation, but a different solution. On my computer running Windows 10 app + the console app WebClient.UploadData

at the address https

worked just fine. But when the same function was copied into an ASP.NET MVC application and published to another web server (Windows 2008 R2), it threw the following exception:

System.Net.WebException: The main connection was closed: An unexpected error occurred while submitting. ---> System.IO.IOException: Unable to read data from 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

Both projects used .NET Framework 4.6.1.



Solved with a challenge TLS1.2

. Add this just before UploadData

:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

      

A source

+18


a source


Thank you, you saved my day. Requests to the Azure AppService were throwing a ".. connection was closed" exception.

Setting SecurityProtocolType.Tls12 fixed the problem.

Also works with

_webClient.DownloadString(url);

      

and

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

      

+1


a source


I have the same problem. Setting the security protocol to TLS1.2 did not fix my problem. I had to add another line of code as suggested here .

System.Net.ServicePointManager.Expect100Continue = false;

      

0


a source







All Articles