6

I'm trying to retrieve a content of HTTP document with MS XMLHTTP COM. I did copied the following sample code but even this does not work and fails with EOLEException error 'Access is denied' at send method call.

uses
  MSXML, ComObj, ActiveX;

procedure TForm1.Button1Click(Sender: TObject);
var
  httpDoc: XMLHTTP;  // IXMLHTTPRequest
begin
    httpDoc := CreateOleObject('MSXML2.XMLHTTP') as XMLHTTP;
  try
    httpDoc.open('GET', 'http://www.google.com/index.html', False, EmptyParam, EmptyParam);
    httpDoc.send('');  // <-- EOLEException 'Access is denied'
    if (httpDoc.readyState = 4) and (httpDoc.status = 200) then
      ShowMessage(httpDoc.responseText);
  finally
    httpDoc := nil;
  end;
end;

I really don't know what am I doing wrong :(

David Unric
  • 7,421
  • 1
  • 37
  • 65

1 Answers1

5

Google does location-based redirections, and sometimes that involves redirecting to another domain. XMLHTTP does not like that. Also, it seems XMLHTTP does not allow access to remote servers when running from a local script (such as from VB, Delphi, etc) outside of a browser. See this discussion, this discussion, and this documentation.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Marked as an answer. Although I have to blame devs of MSXML as they do not offer handling of x-domain redirects cases so programmer is forced to invent some workarounds. Not to mention obscure/non-descriptive error messages. – David Unric Dec 30 '11 at 11:28
  • If you read the articles I linked to, they say you can use the `ServerXMLHTTP` object (http://msdn.microsoft.com/en-us/library/windows/desktop/ms762278.aspx) to accomplish what you are attempting. Also have a look at the `XDomainRequest` object (http://msdn.microsoft.com/en-us/library/dd573303.aspx). – Remy Lebeau Dec 30 '11 at 20:21