Wednesday, January 4, 2012

Verify the URL Link and the File/Folder path in AX


I have experienced to verify the URL link in AX, Currently AX don’t contains any API that validate the URL Link
I have written a method that verifies the URL
public static boolean WebPathExists(str _url)
{
    boolean                    isExists;
    System.Net.WebRequest      UrlWebReq;
    System.Net.WebResponse     UrlWebRes;

    try
    {
        // Encode the URL first before passing to the Create method
        _url = System.Web.HttpUtility::UrlPathEncode(_url);
        UrlWebReq = System.Net.WebRequest::Create(_url);
        // This is needed if secure link is access like the AX share point portal
        // This is works only for the client calls
        UrlWebReq.set_Credentials(System.Net.CredentialCache::get_DefaultCredentials());
         UrlWebRes = UrlWebReq.GetResponse();
        // if there isn't any exception in the response (passed the above line of code) then the path is  correct.
        isExists  = true;
    }
    catch
    {
        isExists = false;
    }

    return isExists;
}

To checks the existence of Windows directory/File Path, there are some methods available in AX WinAPI class

WinAPI::folderExists(_filePath)
WinAPI::fileExists(_folderPath)

4 comments:

  1. Hi,
    I did some test with this method, but I Always get that the url exists even if this not exists.
    Can you please help me with this ?

    thanks in advance
    Iris Q.

    ReplyDelete
  2. Yes, i always get a response, regardingless if the url exists or not

    ReplyDelete
  3. Absolutely great article to read.

    ReplyDelete
  4. I want to show my thanks to you just for rescuing me from such a circumstance. Because of looking throughout the world wide web and coming across thoughts that were not helpful, I recon this blog is the best I've ever read on this topic of Verify the URL link and the file.

    High-quality 2017 Chiropractic Websites for Neuromuscular specialists.

    ReplyDelete