Is there an API that can pull the root domain from a URL?
Or maybe a way to do it with the WebBrowser control?
Or am I going to have to think of some fancy string manipulation?
Printable View
Is there an API that can pull the root domain from a URL?
Or maybe a way to do it with the WebBrowser control?
Or am I going to have to think of some fancy string manipulation?
String manipulation isn't hard. Just look for the third slash... that's the end of the root. Easy code to put together.
not completely right.
For examle"
http://by128fd.bay128.hotmail.msn.co...mbox=00xxxxxxx
the root is msn.com
But there are other, much stranger ways a url can be written.
For that example, you could start at the third slash and work your way back to the 2nd . behind it.
You will also need to take into account that the user may not enter the http:// part. Or may leave out the www. And there may not be a slash after the domain name, ie: hotmail.msn.com.
Exactly, that's why I'd like a nice reliable API or WebBrowser method.Quote:
Originally Posted by DigiRev
It'd be a lot easier and cleaner than trying to think of all the possibilities
Well, it would be pretty simple string manipulation, it would just be a lengthy function, because you have to consider all possibilities.Quote:
Originally Posted by longwolf
To my knowledge, there is no API function for this. And there are no functions available in the WebBrowser control that will give you the root domain name.
I don't have a lot of experience with the WebBrowser control, though, so I could be wrong.
The WebBrowser control has a LOT to it, there just aren't any help files with it.
There are several API calls for working with URLs, and it seems like I saw one once for doing this.
But I can't find it now.
I'm very good at string manipulation, I'm just hopeing for a better option here.
I found it.
VB Code:
Private Declare Function UrlGetPart Lib "shlwapi" Alias "UrlGetPartA" (ByVal pszIn As String, ByVal pszOut As String, pcchOut As Long, ByVal dwPart As Long, ByVal dwFlags As Long) As Long
oops, had the wrong URL, here's the right one:
http://planetsourcecode.com/vb/scrip...27444&lngWId=1
Beats the heck out a class file I found.
It was over 500 lines and that was with it using RegEX!!!!
Nice find. Never knew that API function existed.
Just been playing with it.
I'll still have to do a little string work to get the root domain, but not much.
Turns out there's an easy way to get same thing from the WebBrowser too
VB Code:
Debug.Print wbrBrowser.Document.domain
Found a Good Simple Solution at http://vb6.info/internet/extract-root-url-url-string/
Overall pretty reliable and pretty short compared to other solutions.
at top of module:
thenCode:Private Const CONST_HOSTNAME = 2
Private Declare Function UrlGetPart Lib "shlwapi" Alias "UrlGetPartA" (ByVal pszIn As String, ByVal pszOut As String, pcchOut As Long, ByVal dwPart As Long, ByVal dwFlags As Long) As Long
Code:Public Function sRet(ByVal strUrl As String) As String
On Error GoTo oops:
Dim lngPtr As Long, lngLen As Long
Dim strSection As String, sTemp As String
If (Len(strUrl) = 0) Then Exit Function
strSection = Space$(260&)
lngLen = Len(strSection)
If (UrlGetPart(strUrl, strSection, lngLen, CONST_HOSTNAME, 0&) = 0) Then
sTemp = Left$(strSection, lngLen)
Else
sRet = "error"
Exit Function
End If
'to avoid issues with complicated sub domains, etc, split at second .
sTemp = sfuncRootFromSubDomains(sTemp)
sRet = sTemp
Exit Function
oops:
If err.Number <> 0 Then
Stop '
End If
End Function
Private Function sfuncRootFromSubDomains(ByVal sUrl As String) As String
On Error GoTo oops:
sUrl = Replace(sUrl, "http://", "") 'remove http and https
sUrl = Replace(sUrl, "https://", "")
sUrl = Replace(sUrl, "www.", "") 'remove www.
Dim sParts() As String: sParts = Split(sUrl, ".")
Dim iUpp As Integer: iUpp = UBound(sParts)
sUrl = (sParts(iUpp - 1) & "." & sParts(iUpp))
sfuncRootFromSubDomains = sUrl
Exit Function
oops:
If err.Number <> 0 Then
Stop '
End If
End Function
The planet source code solution has been removed for some reason.
If post #12 is correct seems a lot simpler than post #13
couldn't you split by dots and take the last array element, then split again by slashes and take ... the 2nd?
yes that works IF a web browser is being used.