Hello,
I am trying to make some test web requests out of AX 2012 to an open API, as we need to implement parcel carrier API into our system in near future.
As we don't have experience with C# I've only tested this using .Net Framwork directly from X++ code.
Unfortunately, the HttpWebRequest.GetResponseStream() method doesn't seem to do or return anything. When I debug my class, the programm just stops after executing that method. I've also tried using the WebRequest class, however I get the same result. For better understanding, see my code here:
//Test class for calling an API and printing response
public class IMP_CallRESTfulWebService
{
System.Net.HttpWebRequest webReq;
System.Net.HttpWebResponse webRes;
CLRObject clrObj;
System.IO.Stream stream;
System.IO.StreamReader streamReader;
System.IO.StreamWriter streamWrite;
}
public void run()
{
//Give permission to run unmanaged and managed code
new InteropPermission(InteropKind::ClrInterop).assert();
//Create web request instance
clrObj = System.Net.WebRequest::Create("https://petstore.swagger.io/v2/pet/09021999");
webReq = clrObj;
//Set web request properties
webReq.set_Method("GET");
webReq.set_ContentType("application/x-www-form-urlencoded");
webReq.set_Timeout(10000); //in Miliseconds
//Get the response object
webRes = webReq.GetResponse();
//Print status of response
info(strFmt("%1",webRes.get_StatusCode()));
info(strFmt("%1",webRes.get_StatusDescription()));
//Get content returned by the servers as stream and open that stream using StreamReader for easy access
streamReader = new System.IO.StreamReader(webRes.GetResponseStream());
//The read & print the stream content
info(streamReader.ReadToEnd());
//Cleanup the streams, response and revert permission assert
streamReader.Close();
stream.Close();
webRes.Close();
CodeAccessPermission::revertAssert();
}
main() method just runs the class.
Has anyone experienced similar issues? What could be the problem? Also, is there a way to debug the .Net Framwork method?
And finally, as we are completly new to implementing API's into AX:
Is there maybe a better way to implement an API in AX 2012 R3? The API we are going to implement in the future can either use SOAP or REST and will use XML to send and consume data.
Looking forward to hear any advice,
kind regards