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());
}
}
Just had a very similar problem. AX2012 thrown an System.Net.WebException Unable to connect to remote server on calling GetRequestStream() from HttpWebRequest.
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.
Can you prepare a concrete example that we could run and debug?
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!
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(); } } }
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!
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?
Hi Martin,
The exceptions are been thrown from the line 'stream = httpRequest.GetRequestStream();'.
The type of stream variable is System.IO.Stream.
Thanks!
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.)
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!
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?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,782 Super User 2024 Season 2
Martin Dráb 229,067 Most Valuable Professional
nmaenpaa 101,150