Skip to main content

Notifications

Announcements

No record found.

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

Issue with Http request from AX 2012

(0) ShareShare
ReportReport
Posted on by 242

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());
}


}

  • Radomir Skrabal Profile Picture
    Radomir Skrabal 5 on at
    RE: Issue with Http request from AX 2012

    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.

  • Martin Dráb Profile Picture
    Martin Dráb 230,503 Most Valuable Professional on at
    RE: Issue with Http request from AX 2012

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

  • Suren_G Profile Picture
    Suren_G 242 on at
    RE: Issue with Http request from AX 2012

    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
    Martin Dráb 230,503 Most Valuable Professional on at
    RE: Issue with Http request from AX 2012

    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
    Suren_G 242 on at
    RE: Issue with Http request from AX 2012

    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
    Martin Dráb 230,503 Most Valuable Professional on at
    RE: Issue with Http request from AX 2012

    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
    Suren_G 242 on at
    RE: Issue with Http request from AX 2012

    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
    Martin Dráb 230,503 Most Valuable Professional on at
    RE: Issue with Http request from AX 2012

    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
    Suren_G 242 on at
    RE: Issue with Http request from AX 2012

    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
    Martin Dráb 230,503 Most Valuable Professional on at
    RE: Issue with Http request from AX 2012

    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?

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans