Page 1 of 1

3 big problems of Msxml2.XMLHTTP

Posted: 19 Sep 2015, 04:06
by tmplinshi
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
      }

Re: 3 big problems of Msxml2.XMLHTTP

Posted: 23 Sep 2015, 02:51
by malcev
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.

Re: 3 big problems of Msxml2.XMLHTTP

Posted: 27 Sep 2015, 22:22
by tmplinshi
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.