Reputation: 8764
I am trying to post to our asterisk box to parse out the phone list
from a console application 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 web
request. Please review the stack trace for more information about the error and
where it originated in 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 the page load :
protected void Page_Load(object sender, EventArgs e)
{
var ret = HttpPost();
Response.Write(ret);
}
Upvotes: 12
Views: 9860
Reputation: 131
If you are using .NET Framework 4.0, it only has Tls
and Ssl3
,
namespace System.Net
{
public enum SecurityProtocolType
{
Ssl3 = 48,
Tls = 192,
}
}
You can do this at .NET Framework 4.0 to make it work:
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
for that the Tls12
's code is 3072.
Or just upgrade you project, then you can do the commented thing.
Upvotes: 0
Reputation: 185
I had 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;
Upvotes: 1
Reputation: 31
Thanks, you saved my day. Requests to an Azure AppService app was throwing the ".. 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();
Upvotes: 2
Reputation: 4232
I had very similar situation but different solution. On my Windows 10 dev machine+console app, the WebClient.UploadData
to a https
address was working just fine. But when same exact function was copied to a ASP.NET MVC app, and published to different web server (Windows 2008 R2) it was giving this exception:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send . ---> System.IO.IOException: Unable to read data from the 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 were using .NET framework 4.6.1
Solved by making the call use TLS1.2
. Add this just before UploadData
:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Upvotes: 33
Reputation: 8764
It was a dns issue ... the server was resolving to the private ip console app was resolving to public
Upvotes: 2