I am getting error when trying to consume REST API from Dynamics AX 2009.
"The underlying connection was closed: An unexpected error occurred on a send. Received an unexpected EOF or 0 bytes from the transport stream. in ax 2009"
static void IntegrationTest(Args _args)
{
System.Net net;
System.Security.Authentication Auth;
str url;
str method;
str header;
System.Net.HttpWebRequest httpRequest;
System.Net.HttpWebResponse httpResponse;
System.Net.WebHeaderCollection headers;
CLRObject clro;
System.Net.SecurityProtocolType spt;
System.Net.ServicePoint servicePt;
int batchSize = 1024;
System.IO.Stream receiveStream;
System.IO.StreamReader readStream;
System.Text.Encoding encode;
System.Char[] read;
System.Text.StringBuilder sb;
System.String readString;
str contentEncoding;
int countRead;
// url = strfmt("">XXXXXXXXXX/.../session");
method = "POST";
try
{
clro = System.Net.WebRequest::Create("">XXXXXXXXXXXXX/.../session");
httpRequest = clro;
headers = new System.Net.WebHeaderCollection();
headers.Add("username", "XXXXX");
headers.Add("password", "XXXXX");
httpRequest.set_Headers(headers);
httpRequest.set_Method(method);
httpRequest.set_ContentType("application/json");
//Third try start to fix underlying connection
servicePt = httpRequest.get_ServicePoint();
servicePt.set_Expect100Continue(false);
//Third try Ends to fix underlying connection
//First try start to fix underlying connection
// System.Net.ServicePointManager::set_Expect100Continue(false);
// System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::TLS12);
//First Try End to fix underlying connection
//Second try start to fix underlying connection
//spt = System.Net.SecurityProtocolType::Tls;
//System.Net.ServicePointManager::set_SecurityProtocol(spt);
//Second try End to fix underlying connection
info("Start");
httpResponse = httpRequest.GetResponse();
// Above line gives error "The underlying connection was closed: An unexpected error occurred on a send. Received an unexpected EOF or 0 bytes from the transport stream. in ax 2009"
info("Ënd")
if (httpResponse)
{
receiveStream = httpResponse.GetResponseStream();
contentEncoding = httpResponse.get_ContentEncoding();
if (contentEncoding)
{
encode = System.Text.Encoding::GetEncoding(contentEncoding);
}
else
{
encode = new System.Text.UTF8Encoding();
}
readStream = new System.IO.StreamReader(receiveStream, encode);
read = new System.Char[batchSize]();
countRead = readStream.Read(read, 0, batchSize);
sb = new System.Text.StringBuilder();
while (countRead > 0)
{
readString = new System.String(read, 0, countRead);
sb.Append(readString);
countRead = readStream.Read(read, 0, batchSize);
}
readStream.Close();
info(sb.ToString());
}
}
catch(Exception::CLRError)
{
throw error(AifUtil::getClrErrorMessage());
}
}
in postman it works with below
Method : Post
URL:- XXXXXXXXX/.../session
body -> Raw-Json