I am doing a webservice call from D365 and get below error message:
The underlying connection was closed: An unexpected error occurred on a receive.
In test it works fine but in production we get this error.
We also found out SecurityProtocalType had to be set to TLS otherwise it did not work in production. This worked without TLS in test.
The code is below
protected str callCOM(str _type, str _httpAddress)
{
str ret;
{
str ret;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.IO.Stream streamstr,responsestr;
System.IO.StreamReader streamReader;
System.Net.WebHeaderCollection httpHeader;
System.Byte[] bytes;
CLRObject clrObj;
System.Net.HttpWebResponse response;
System.IO.Stream streamstr,responsestr;
System.IO.StreamReader streamReader;
System.Net.WebHeaderCollection httpHeader;
System.Byte[] bytes;
CLRObject clrObj;
new InteropPermission(InteropKind::ClrInterop).assert();
System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls);
clrObj = System.Net.WebRequest::CreateHttp(_httpAddress);
request = clrObj;
request.set_Method(_type);
request.set_ContentType('application/xml; charset=utf-8');
request.set_KeepAlive(false);
request.set_Timeout(5000);
request.set_ContentLength(0);
clrObj = System.Net.WebRequest::CreateHttp(_httpAddress);
request = clrObj;
request.set_Method(_type);
request.set_ContentType('application/xml; charset=utf-8');
request.set_KeepAlive(false);
request.set_Timeout(5000);
request.set_ContentLength(0);
httpHeader = request.get_Headers();
httpHeader.Add('Authorization','Bearer '+B2bParameters::find().Password);
httpHeader.Add('Authorization','Bearer '+B2bParameters::find().Password);
if (_type!='GET')
{
bytes = System.Text.Encoding::get_UTF8().getBytes(xml.getText());
request.set_ContentLength(bytes.get_Length());
{
bytes = System.Text.Encoding::get_UTF8().getBytes(xml.getText());
request.set_ContentLength(bytes.get_Length());
streamstr = request.GetRequestStream();
streamstr.Write(bytes,0,bytes.Get_Length());
}
else
{
response = request.GetResponse();
responsestr = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responsestr);
ret = streamReader.ReadToEnd();
}
CodeAccessPermission::revertAssert();
return ret;
}
streamstr.Write(bytes,0,bytes.Get_Length());
}
else
{
response = request.GetResponse();
responsestr = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responsestr);
ret = streamReader.ReadToEnd();
}
CodeAccessPermission::revertAssert();
return ret;
}