• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to get the URL address back using any command in xmlhttp requests?

shahin

Active Member
How to get the "URL" address back using xmlhttp request? I did it with IE and found the URL back successfully but in case of sending the request with xmlhttp I get a blank URL. How can I achieve that?

Using IE:
Code:
Sub fetch_url()
    Dim IE As New InternetExplorer, html As New HTMLDocument

    With IE
        .Visible = False
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do While IE.readyState <> READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    MsgBox html.URL
    IE.Quit
End Sub

The result I get is:
Code:
https://chandoo.org/forum/forums/vba-macros/

However, using xmlhttp requests:
Code:
Sub WebData()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim source As Object

    With http
        .Open "GET", "https://chandoo.org/forum/forums/vba-macros/", False
        .send
        html.body.innerHTML = .responseText
    End With
    MsgBox html.URL
End Sub

Result:
Code:
about:blank

To be clearer: I expect to get the same url back as a result using xmlhttp request.
 
FYI - This may not be what you are looking for. But this is usually how I would load head to html.

Code:
Sub WebData()
    Dim source As Object, resp As String
    With CreateObject("MSXML2.xmlHttp")
        .Open "GET", "https://chandoo.org/forum/forums/vba-macros/", False
        .send
        resp = .responseText
    End With

    With CreateObject("HTMLFile")
        .write resp
        For Each obj In .all(2).getElementsByTagName("META")
            If obj.Content Like "https:/*/" Then
                Debug.Print obj.Content
            End If
        Next
    End With
End Sub

Note: HTMLFile must be late bound, otherwise ".write" will throw error "...as restricted, or the function uses Automation type not supported in VB". "MSXML2" need not be late bound.
 
Last edited:
@ sir chihiro, You just made impossible possible. It worked with few sites. I'm thrilled that there is something in vba (xmlhttp request) as well to do the same as i expected. You are almost there. Btw, do you think it is possible (foolproof)?
 
As I always say, there isn't single code that will handle every site. Each site uses their own variation in structure and coding. As always, first step is to study .responseText and see where the info you are looking for is stored.
 
Better late than never. I have got three questions about the revolutionary code you have provided above in post #3 @sir Chihiro.


1. I could not understand the ".all()" which has been used in this line ".all(2).getElementsByTagName("META")".

2. I'm familiar with ".innertext" and ".textContent" but what is this ".Content" for?

3. I'm not familiar with "Like" operator either. Does it work like "Instr()" function?


Thanks a lot in advance sir. All the questions I've asked above may not be ideal questions at all. However, they all mean a lot to me.
 
Hi !

May be easier with WinHttp request (see its options property) …

1) Set an object variable on all collection then see in Locals window …

2) See the object also in Locals window …

3) No, easy just reading VBA inner help !

Observing, reading : the good habits of any programmer !
 
Hi Marc L, it's an immense pleasure to hear something from you. The thing is I get frightened with the name of "locals window", "watch window" etc. I'm only familiar with "immediate window". I'm eager to learn about them, though.
 
1. .All() as name suggest, returns collection of all elements within specified object. Read link for detail.
https://msdn.microsoft.com/en-us/library/office/aa209138(v=office.11).aspx

2. It reads meta content.
https://www.w3schools.com/jsref/prop_meta_content.asp

3. ... just search VBA Like Operator in Google search or some other search engine.

The thing is I get frightened with the name of "locals window", "watch window" etc.

I gave you link to Luke M's post with explanation to this. If you are not willing to help your self... :Shrug:
 
The two links seem to be super helpful. I will read them out. However, recently I've found out that using "HTMLDivElement", it is possible to work with "class" names as well even if there is a "late binding". I'm posting the below code because I knew so far that using "late binding" it is only possible to play with tag names but in reality it is not..

Code:
Sub Web_Data()
    Dim HTML As New HTMLDocument, post As HTMLDivElement
  
    With CreateObject("MSXML2.serverXMLHTTP")
        .Open "GET", "https://yts.ag/browse-movies", False
        .send
        HTML.body.innerHTML = .responseText
    End With
  
    For Each post In HTML.getElementsByClassName("browse-movie-bottom")
        With post.getElementsByClassName("browse-movie-title")
            If .Length Then Row = Row + 1: Cells(Row, 1) = .Item(0).innerText
        End With
    Next post
End Sub
 
It's not issue of late bind... it's whether reference to external library is added or not...

If you can declare "post As HTMLDivElement" then you have reference to HTML Object library added...
 
Oh I see!! Btw, you might have noticed that using "post as Object", It's not possible to access class names.
 
It's difference between how you declared the variable.

If declared as Object, Post can be any object type. When explicitly defined as HTMLDivElement it can only be that object type. Hence the difference when you try to loop post object.
 
Back
Top