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)

5 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
    Replies
    1. So basically every server can be configured in a different way, So for instance a wrong url can be handled via the server to get a succesful response.
      Well to be absolutely sure about a valid url we need to request the http status code as well as ban redirection:
      public static boolean web_checkURL(URL _url, boolean _printError = false)
      {
      boolean result = false;
      System.Net.HttpWebRequest request;
      System.Net.HttpWebResponse response;
      int status;
      String255 description;

      try
      {
      request = System.Net.WebRequest::Create(_url) as System.Net.HttpWebRequest;
      request.set_Timeout(5000);
      request.set_Method('GET');
      request.set_AllowAutoRedirect(false);
      request.set_Credentials(System.Net.CredentialCache::get_DefaultCredentials());

      response = request.GetResponse();

      if(!response)
      return false;

      //http://msdn.microsoft.com/de-de/library/system.net.httpstatuscode%28v=vs.110%29.aspx
      if(response.get_StatusCode() != System.Net.HttpStatusCode::OK && response.get_StatusCode() != System.Net.HttpStatusCode::Accepted)
      {
      if(_printError)
      {
      status = response.get_StatusCode();
      description = response.get_StatusDescription();
      error(strFmt('URL %1, HttpStatusCode %2, StatusDescription %3', _url, status, description));
      }

      return false;
      }

      response.Close();

      result = true;
      }
      catch
      {
      result = false;
      }

      return result;
      }

      Delete
  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