Failing while trying to read content from URL via InternetExplorer in C#

464 views Asked by At

I have a strange behavior using InternetExplorer Object to read information from http sites:

My Code so far:

string url = "http://www.someURL.com"
InternetExplorer IE = new InternetExplorer();
IE.Visible = true;
IE.Navigate(url);
while (IE.Busy == true) {
     System.Threading.Thread.Sleep(500);
}

IHTMLDocument2 htmlDoc = IE.Document as IHTMLDocument2;
string sourceCode = htmlDoc.body.outerHTML;
IE.Quit();

This code works for some Webistes, for some it doesnt. For those which doesnt, it fails with

System.Runtime.InteropServices.COMException

HRESULT E_FAIL

I found a workaround for those websites, but i am not happy with that:

static private SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer ii in shellWindows) {
        //Check URL or Title to figure out if this is my window
}

When i have more windows open with the same Title or URL i do not know which one is the one i opened before.

Unfortunatelly i can not share the not working URL because it is not reachable from outside my infrastructure.

0

There are 0 answers