Hi, Readers.
Today I would like share a mini tip, how to quickly get host name from URL via AL.
As you might know, A URL (Uniform Resource Locator) is a unique identifier used to locate a resource on the Internet. And in the Internet, a hostname is a domain name assigned to a host computer. This is usually a combination of the host’s local name with its parent domain’s name. For example, en.wikipedia.org consists of a local hostname (en) and the domain name wikipedia.org. More details: wiki | Hostname.
For example, for https://www.microsoft.com/, the host name is www.microsoft.com.
Let’s see another example.
When we use the standard APIs, the host name is api.businesscentral.dynamics.com.
So how to get the host name via AL? Of course you can consider using String Functions, such as StrPos, CopyStr, StrLen……., to split from the URL. But please remember, you have to verify that the URL is valid before splitting, it’s not that easy. So this is not recommended.
And there is actually a standard way to do this.
In codeunit 1299 “Web Request Helper”:
Let me do a simple test.
Source Code:
page 50111 GetHostNameFromURL
{
Caption = 'Get Host Name From URL';
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(General)
{
field(URL; URL)
{
Caption = 'URL';
ApplicationArea = All;
}
field(HostName; HostName)
{
Caption = 'Host Name';
ApplicationArea = All;
Editable = false;
}
}
}
}
actions
{
area(Processing)
{
action(GetHostName)
{
ApplicationArea = All;
Caption = 'Get Host Name';
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
trigger OnAction()
var
WebRequestHelper: Codeunit "Web Request Helper";
begin
HostName := WebRequestHelper.GetHostNameFromUrl(URL);
end;
}
}
}
var
URL: Text;
HostName: Text;
}
Looks good😁
When the URL is not valid:
Test video:
END
Hope this will help.
Thanks for reading.
ZHU
コメント