Du kan jo køre browser synkront:
Browser.Navigate("
www.dfdfd.dk");
while( Browser.ReadyState != ReadyState.Completed )
Application.DoEvents();
Hvorfor du kan få fa i koden med webbrowser og ikke med HttpWebRequest skyldes formentlig redirects eller javascripts som HttpWebRequest ikke fanger.
I stedet for at anvende browser vil jeg dog anbefale dig at kigge på CreateDocumentFromUrl, ingen grund til at have en webbrowser i sin kode med mindre du skal vise noget:
using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using mshtml;
namespace Common.Libraries.Html
{
public class HTMLDocumentUtil
{
public XmlDocument GetXmlDocument(string html)
{
return null;
}
public mshtml.IHTMLDocument2 LoadHtmlDocument(string html)
{
mshtml.IHTMLDocument2 doc = new mshtml.HTMLDocumentClass();
html = html.Replace("window.", "window..");
html = html.Replace("document.", "document..");
html = html.Replace(".submit()", "submit().");
html = html.Replace(".open", "..open");
html = html.Replace(".show", "..show");
html = html.Replace(".location", "..location");
int i = html.ToLower().IndexOf("<head>");
if (i != -1)
html = html.Insert(i + 6, "<script type=text/javascript>function handleError() {return true;}window.onerror = handleError;</script>");
doc.write(new object[] { html });
while (doc.readyState != "complete")
System.Windows.Forms.Application.DoEvents();
return doc;
}
public mshtml.IHTMLDocument2 CreateHtmlDocument(string url)
{
mshtml.HTMLDocument objMSHTML = new mshtml.HTMLDocument();
mshtml.IHTMLDocument2 objDocument;
IPersistStreamInit ips;
ips = ((IPersistStreamInit)objMSHTML);
ips.InitNew();
objDocument = objMSHTML.createDocumentFromUrl(url, null);
if (objDocument == null)
return null;
while (objDocument.readyState != "complete")
{
System.Windows.Forms.Application.DoEvents();
}
return objDocument;
}
public IHTMLElement GetElementFromPath(IHTMLDocument2 doc, string path)
{
IHTMLElementCollection hec = (IHTMLElementCollection)doc.body.children;
IHTMLElement hElement = null;
FindElement(path, hec, hElement);
return hElement;
}
void FindElement(string path, mshtml.IHTMLElementCollection hec, IHTMLElement element)
{
string tagName = getTagFromPath(path);
int nr = getTagNumberFromPath(path);
int counter = 0;
foreach (IHTMLElement he in hec)
{
if (counter == nr && he.tagName == tagName)
{
string s = trimPath(path);
if (s.Length > 1)
{
FindElement(s, (mshtml.IHTMLElementCollection)he.children, element);
break;
}
else
{
element = he;
return;
}
}
else
{
if (he.tagName == tagName)
{
counter++;
}
}
}
}
string trimPath(String path)
{
string p = path.Substring(path.IndexOf('.') + 1, path.Length - path.IndexOf('.') - 1);
return p;
}
string getTagFromPath(string path)
{
return path.Substring(0, path.IndexOf('['));
}
int getTagNumberFromPath(string path)
{
string nr = System.Text.RegularExpressions.Regex.Match(path, @"^(\w+)\[(\d+)\]").Groups[2].Value;
string s1 = System.Text.RegularExpressions.Regex.Match(path, @"^(\w+)\[(\d+)\]").Groups[1].Value;
return Int32.Parse(nr);
}
}
[ComVisible(true), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersistStreamInit
{
void GetClassID(ref Guid pClassID);
[PreserveSig()]
int IsDirty();
[PreserveSig()]
int Load(UCOMIStream pstm);
[PreserveSig()]
int Save(UCOMIStream pstm, bool ByValfClearDirty);
[PreserveSig()]
int GetSizeMax([InAttribute(), Out(), MarshalAs(UnmanagedType.U8)] ref long pcbSize);
[PreserveSig()]
int InitNew();
}
}
Husk at tilføje mshtml.dll til dine referencer.