Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

How to consume REST API in AX 2009 ?

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
    SC-19081230-0 233 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans