In this post will explain about how to receive data from Rest API.. Here you i attached some sample code receiving data from Rest API in Dynamics ax 2009...
Sample Code:
Public void receiveClientFromCrm()
{
System.Net.HttpWebRequest webReq;
System.Net.HttpWebResponse webRes;
CLRObject clrObj;
System.IO.Stream stream;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePt;
xml responseXml;
;
//This line gives the user control to run unmanaged and managed code
CodeAccessPermission::revertAssert();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj =
System.Net.WebRequest::Create('your api url with accesskey");
webReq = clrObj;
//Set Method Type
webReq.set_Method('GET');
webReq.set_KeepAlive(true);
// Set Content type
webReq.set_ContentType('application/xml');
//Get Service Point
servicePt = webReq.get_ServicePoint();
servicePt.set_Expect100Continue(false);
//Gets the response object
webRes = webReq.GetResponse();
//Get Stream
stream = webRes.GetResponseStream();
// BP Deviation documented
streamRead = new System.IO.StreamReader(stream);
responseXml = streamRead.ReadToEnd();
//writing the xml from info to file using streamwrite
// BP Deviation documented
streamWrite = new System.IO.StreamWriter("File Directory Location for saving with particular file pattern");
streamWrite.WriteLine(responseXml);
// BP Deviation documented
codeAccessPermission::revertAssert();
streamWrite.Flush();
streamWrite.Close();
streamWrite.Dispose();
}
Sample Code:
Public void receiveClientFromCrm()
{
System.Net.HttpWebRequest webReq;
System.Net.HttpWebResponse webRes;
CLRObject clrObj;
System.IO.Stream stream;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePt;
xml responseXml;
;
//This line gives the user control to run unmanaged and managed code
CodeAccessPermission::revertAssert();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj =
System.Net.WebRequest::Create('your api url with accesskey");
webReq = clrObj;
//Set Method Type
webReq.set_Method('GET');
webReq.set_KeepAlive(true);
// Set Content type
webReq.set_ContentType('application/xml');
//Get Service Point
servicePt = webReq.get_ServicePoint();
servicePt.set_Expect100Continue(false);
//Gets the response object
webRes = webReq.GetResponse();
//Get Stream
stream = webRes.GetResponseStream();
// BP Deviation documented
streamRead = new System.IO.StreamReader(stream);
responseXml = streamRead.ReadToEnd();
//writing the xml from info to file using streamwrite
// BP Deviation documented
streamWrite = new System.IO.StreamWriter("File Directory Location for saving with particular file pattern");
streamWrite.WriteLine(responseXml);
// BP Deviation documented
codeAccessPermission::revertAssert();
streamWrite.Flush();
streamWrite.Close();
streamWrite.Dispose();
}
No comments:
Post a Comment