Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How to consume REST API in AX 2009 ?

(0) ShareShare
ReportReport
Posted on by 410

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

{
 "username""XXXXX",
 "password""XXXX"
}
In my environment we have TLS only not TLS 1.2 
  • SC-19081230-0 Profile Picture
    5 on at
    How to consume REST API in AX 2009 ?
    I have - suddenly - the same problems. 
    Was there any solution for this? 
     
    Søren PC

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans