Thursday, October 29, 2009

AX: HTTP Web Request to URL

//This method makes HTTP web request to the given url and returns the response


Static XML HttpWebRequest(XML _url, XML _parameter = "")

{

System.Net.WebRequest webrequest;

System.Net.HttpWebResponse httpresponse;

System.IO.Stream stream;

System.IO.StreamReader streamReader;

xml responseXML;

System.Byte[] arrayOfBytes;

System.Text.Encoding encoding;

;

try

{

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

//Use .Net framework reflections to make HttpRequest and get the response back

encoding = System.Text.Encoding::get_UTF8();

arrayOfBytes = encoding.GetBytes(_parameter);

webrequest = System.Net.WebRequest::Create(_url);

webrequest.set_Method("POST");

webrequest.set_ContentType("application/x-www-form-urlencoded");

webrequest.set_ContentLength(arrayOfBytes.get_Length());

stream = webrequest.GetRequestStream();

stream.Write(arrayOfBytes,0,arrayOfBytes.get_Length());

stream.Close ();



httpresponse = webrequest.GetResponse();

stream = httpresponse.GetResponseStream ();

streamReader = new System.IO.StreamReader (stream);

responseXML = streamReader.ReadToEnd ();

streamReader.Close ();

stream.Close ();

httpResponse.Close ();



codeAccessPermission::revertAssert();

}

catch

{

throw error(strfmt("Exception occured during payment process. Url: %1", _url));

}

return responseXML;

}