I am trying to make a call to an API within a SSIS package. I am able to use the same code in a regular unit test class and everything works as expected. I tried some of the recommendations I've seen in stack overflow but no luck.
It fails at the GetRequestStream()
Error: The underlying connection was closed: An unexpected error occurred on a send. Inner Error Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Code:
var request = (HttpWebRequest)WebRequest.Create(requestURL);
var muaRequest = new MUARequest
{
designationType = "MUAP"
};
var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(muaRequest));
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = Timeout.Infinite;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
//var response = (HttpWebResponse)request.GetResponse();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
var content = reader.ReadToEnd();
results = JsonConvert.DeserializeObject<Results>(content);
}
}
}
else
{
results.ErrorCode = "Http Request Failed.";
}
}
The declaration of the security protocol needs to be called before the creation of the web request. After that it works