Answer the question
In order to leave comments, you need to log in
How to solve the problem with POST request over SSL protocol?
Since yesterday, when making a POST request from a C # application to the site, I began to receive an error:
The underlying connection was closed: An unexpected error occurred on a send.
using System;
using System.Collections.Specialized;
using System.Net;
using System.Security.Authentication;
using Kstudio.Manager;
using Kstudio.Manager.Settings;
namespace PostRequestTest
{
public static class Program
{
static void Main(string[] args)
{
UpdateStart("https");
UpdateStart("http");
Console.ReadLine();
}
private static void UpdateStart(string protocol)
{
Console.WriteLine("Sending a POST data through " + protocol.ToUpper());
WebInfo.Protocol = protocol;
ServicePointManager.MaxServicePointIdleTime = 5000 * 10;
ServicePointManager.Expect100Continue = false;
try
{
var result = GetUpdateVersion();
//result = result.Remove(result.Length/2);
if (!string.IsNullOrEmpty(result))
result = result.Remove(result.Length / 100) + "...\n\n Success!!";
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Console.WriteLine("====================\n" );
}
public static string GetUpdateVersion()
{
var req = new ExtWebClient
{
Encoding = System.Text.Encoding.UTF8,
PostParam = new NameValueCollection()
};
req.PostParam.Add("data", "somedata");
req.PostParam.Add("product", 1.ToString());
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
try
{
byte[] result = req.DownloadData(new System.Uri(WebInfo.CheckUpdateUrl));
return DeserializeData(result);
}
catch (WebException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
catch (AuthenticationException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
return null;
}
private static string DeserializeData(byte[] result)
{
if (result == null || result.Length == 0)
return string.Empty;
return System.Text.Encoding.UTF8.GetString(result, 1, result.Length - 1);
}
}
}
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
namespace Kstudio.Manager
{
internal sealed class ExtWebClient : WebClient
{
public NameValueCollection PostParam { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest tmprequest = base.GetWebRequest(address);
HttpWebRequest request = tmprequest as HttpWebRequest;
//Setting KeepAlive to false
if (request != null)
request.KeepAlive = false;
if (request != null && PostParam != null && PostParam.Count > 0)
{
StringBuilder postBuilder = new StringBuilder();
request.Method = "POST";
//build the post string
for (int i = 0; i < PostParam.Count; i++)
{
var key = PostParam.GetKey(i);
var value = PostParam.Get(i);
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
continue;
postBuilder.AppendFormat("{0}={1}", Uri.EscapeDataString(key),
Uri.EscapeDataString(value));
if (i < PostParam.Count - 1)
{
postBuilder.Append("&");
}
}
byte[] postBytes = Encoding.ASCII.GetBytes(postBuilder.ToString());
request.ContentLength = postBytes.Length;
//request.Accept = "application/json";
//request.UserAgent = "curl/7.37.0";
request.ContentType = "application/x-www-form-urlencoded";
var stream = request.GetRequestStream();
if (stream != null)
{
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
stream.Dispose();
}
}
return tmprequest;
}
}
}
Answer the question
In order to leave comments, you need to log in
The problem "by itself" disappeared after a couple of days.
Perhaps the hoster corrected something or there was a problem with the provider - this explains why my different computers gave one error and there was only one complaint from the client.
Anyway, thank you all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question