3 big problems of Msxml2.XMLHTTP

Put simple Tips and Tricks that are not entire Tutorials in this forum
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

3 big problems of Msxml2.XMLHTTP

Post by tmplinshi » 19 Sep 2015, 04:06

Msxml2.XMLHTTP is similar to WinHttp.WinHttpRequest.5.1, BUT there are 3 big problems you should know:
  1. It's not be able to redirect to different domain.
    For example, the url http://alturl.com/it3jp will redirect to http://example.com/

    Code: Select all

    req := ComObjCreate("Msxml2.XMLHTTP")
    req.open("GET", "http://alturl.com/it3jp", False)
    req.Send()
    MsgBox, % req.responseText "`n" req.getAllResponseHeaders() ; Get nothing
  2. It's not be able to set Referer header.
    req.SetRequestHeader("Referer", "http://example.com/") just doesn't work, you can try it yourself.
  3. It will caching the contents of the URL page.
    Which means if you request the same URL more than once, you always get the same responseText even the website changes text every time.
    Example:

    Code: Select all

    req := ComObjCreate("Msxml2.XMLHTTP")
    Loop, 3 {
    	req.open("GET", "http://tmplinshi.sinaapp.com/test/time.php", False)
    	req.Send()
    	MsgBox, % req.responseText ; You'll get the same time each time.
    }
    With some Googling, I found two solutions.
    1. Add a random parameter to the url.

      Code: Select all

      req := ComObjCreate("Msxml2.XMLHTTP")
      Loop, 3 {
      	r := ComObjCreate("Scriptlet.TypeLib").Guid
      	req.open("GET", "http://tmplinshi.sinaapp.com/test/time.php?r=" r, False)
      	req.Send()
      	MsgBox, % req.responseText
      }
    2. Set cache related headers.

      Code: Select all

      req := ComObjCreate("Msxml2.XMLHTTP")
      Loop, 3 {
      	req.open("GET", "http://tmplinshi.sinaapp.com/test/time.php", False)
      	;req.SetRequestHeader("Pragma", "no-cache")
      	;req.SetRequestHeader("Cache-Control", "no-cache")
      	req.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
      	req.Send()
      	MsgBox, % req.responseText
      }
Last edited by tmplinshi on 03 Mar 2017, 23:31, edited 1 time in total.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: 3 big problems of Msxml2.XMLHTTP

Post by malcev » 23 Sep 2015, 02:51

But what the advantages to use Msxml2.XMLHTTP instead of WinHttp.WinHttpRequest.5.1?
And where I can read about it. Here?
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
If yes, Could You please give example how to use:
1) addEventListener: Registers an event handler for the specified event type.
2) dispatchEvent: Sends an event to the current element.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 3 big problems of Msxml2.XMLHTTP

Post by tmplinshi » 27 Sep 2015, 22:22

Msxml2.XMLHTTP uses the IE Browser's cookies. That's the main reason I use it. But most of the time I use WinHttp.WinHttpRequest.5.1.

Msxml2.XMLHTTP is not same as XMLHttpRequest(). There's no addEventListener and dispatchEvent methods in Msxml2.XMLHTTP.

Post Reply

Return to “Tips and Tricks (v1)”