web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Issue with Http request from AX 2012

(0) ShareShare
ReportReport
Posted on by 252

Hi,

I am trying to trigger an logic app URL in AX using HTTP request. Below is the code that I am using in AX. The code was working fine couple of weeks ago and suddenly stopped working with no change in the logic. Could anyone please share your thoughts regrading the issue.

public static void test()
{
System.Net.HttpWebRequest httpRequest = null;
System.Net.HttpWebResponse httpResponse = null;
System.Net.CookieCollection cookies = null;
CLRObject clro = null;
System.Text.Encoding utf8 = null;
System.Net.WebHeaderCollection headers;
System.IO.Stream stream = null;
System.IO.StreamReader streamReader = null;
System.Byte[] byteArray = null;
System.IO.Stream dataStream;
str jsonData,data,url;
int num;
;

try

{

new InteropPermission(InteropKind::ClrInterop).assert();


jsonData = '{"Name":"'+'Test'+'"}';

headers = new System.Net.WebHeaderCollection();

clro = System.Net.WebRequest::Create(URL);
httpRequest = clro;
httpRequest.set_Method('Post');
httpRequest.set_Timeout(90000);
httpRequest.set_Headers(headers);
httpRequest.set_ContentType("application/json");

utf8 = System.Text.Encoding::get_UTF8();
byteArray = utf8.GetBytes(jsonData);
httpRequest.set_ContentLength(byteArray.get_Length());
stream = httpRequest.GetRequestStream();
stream.Write(byteArray,0,byteArray.get_Length());
stream.Close ();


httpResponse = httpRequest.GetResponse();
}
catch (Exception::CLRError)
{
throw error(AIFUtil::getClrErrorMessage());
}


}

I have the same question (0)
  • Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at

    You'll have to give us at least some information about your problem. For instance, do you get an exception? If so, what's the type of the exception and the message?

  • Suren_G Profile Picture
    252 on at

    Hi Martin,

    Stream buffer has the below exception

    Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException'

    Position = 'dataStream.Position' threw an exception of type 'System.NotSupportedException'

    and post execution is

    The remote server returned an error: (400) Bad Request.

    Thanks!

  • Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at

    Which methods throw those exceptions?

    What's the actual type of stream variable? (System.IO.Stream is an abstract class; I'm interested which concrete subclass you have there.)

  • Suren_G Profile Picture
    252 on at

    Hi Martin,

    The exceptions are been thrown from the line 'stream = httpRequest.GetRequestStream();'.

    The type of stream variable is System.IO.Stream.

    Thanks!

  • Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at

    If GetRequestStream() fails, you don't get any stream object at all, do you?

    As I mentioned, it's impossible to create any instance of the Stream class, because it's abstract. All streams are instance of non-abstract classes extending Stream, such as MemoryStream.

    By the way, what's the actual type httpRequest? HttpWebRequest?

  • Suren_G Profile Picture
    252 on at

    Hi Martin,

    Yes, There is no stream object coming up. It is HttpWebRequest.

    And this code perfectly worked before and I am not sure why it stopped suddenly.

    Thanks!

  • Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at

    What if you try the same thing in a console application (C#)? It might show that the problem isn't related to AX, or that the logic is fine but something weird happen in AX.

    Here is your code rewritten to C#:

    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string url = "...";
                string jsonData = "{'Name': 'Test'}";
    
                WebRequest request = WebRequest.Create(url);
                request.Method = "POST";
                request.Timeout = 90000;
                request.ContentType = "application/json";
    
                byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
                request.ContentLength = byteArray.Length;
    
                Stream stream = request.GetRequestStream();
                stream.Write(byteArray, 0, byteArray.Length);
                stream.Close();
    
                WebResponse response = request.GetResponse();
    
            }
        }
    }
    

  • Suren_G Profile Picture
    252 on at

    Hi Martin,

    The C# code works perfectly. I am able to get a response. Any thoughts on why it doesn't works in AX?

    Thanks!

  • Martin Dráb Profile Picture
    237,904 Most Valuable Professional on at

    Can you prepare a concrete example that we could run and debug?

  • Radomir Skrabal Profile Picture
    5 on at

    Just had a very similar problem. AX2012 thrown an System.Net.WebException Unable to connect to remote server on calling GetRequestStream() from HttpWebRequest.

    pastedimage1637849925427v1.png

    Not sure if it will help, however in my case, this was actually caused by the web service. Service I was calling was still in development and was not always reachable/responding correctly. When I tried to substitute address for public service I knew was working, call to GetRequestStream() went without issue.

    It almost seem like GetRequestStream() is actually calling the webservice in some way. Which seem a little weird, however my knowledge in that field is limited.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans