Here is the code from msdn, please consult if it can answer your question
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local); foreach (SPWebService webService in webServices) { foreach (SPWebApplication webApp in webService.WebApplications) { if (!webApp.IsAdministrationWebApplication) { get the URL here }
source share