Is it possible to add an event handler for SHDocVw.WebBrowser or use AxSHDocVw.WebBroswer without a

  • Thread starter Thread starter David
  • Start date Start date
D

David

I would like to use a AxSHDocVw.WebBroswer control in a Windows service- is
this possible without creating a form? I added the reference I usually use
to add this object in a Windows Application but only SHDocVw appears under
References in the Solution Explorer.

As an alternative, SHDocVw.WebBrowser may work but I am not sure how to add
an event handler for it- does anyone know how to add a DocCompleted event
for this object?
 
David said:
I would like to use a AxSHDocVw.WebBroswer control in a Windows service- is
this possible without creating a form? I added the reference I usually use
to add this object in a Windows Application but only SHDocVw appears under
References in the Solution Explorer.


I wonder why you would want to use a visual control in a service which
doesn't provide a user interface.

* What exactly do you want to archieve?
* Which version of .NET are you using?
 
David,

Not tested however maybe you can try it by using instead of x.webbrowser
x.InterNetExplorer that normaly opens the standard browser.

Cor
 
Herfried K. Wagner said:
I wonder why you would want to use a visual control in a service which
doesn't provide a user interface.

* What exactly do you want to archieve?
* Which version of .NET are you using?

I'm writing a service that will connect to a website on a regular basis,
retrieve information, and then update external devices- this works fine as
an application but I would rather not use task scheduler to run it and a
service sounds like the best method for this. I started out using
HttpWebRequest but was not successful logging into the website with it-
axWebBrowser worked nicely.

I tried using the common component WebBrowser instead of the ActiveX one but
ran into problems accessing the DOM. I tried creating a HTMLDocument
variable and setting it equal to WebBrowser.Document but encountered a
"specified cast is not valid" error. Using the debugger to look at the
WebBrowser.Document property lead me to a "cannot obtain value" message- the
WebBrowser had loaded a webpage so I'm stuck there with that one, and stuck
with the event handler on SHDocVw.WebBrowser.
 
David said:
I'm writing a service that will connect to a website on a regular basis,
retrieve information, and then update external devices- this works fine as
an application but I would rather not use task scheduler to run it and a
service sounds like the best method for this. I started out using
HttpWebRequest but was not successful logging into the website with it-
axWebBrowser worked nicely.

Which authentication model does the Web site use?

Maybe this code snippet is of use for you:

<URL:http://dotnet.mvps.org/dotnet/code/net/#CookieRequest>

Otherwise take a look at the 'NetworkCredentials' class.

I would not use the webbrowser control for this purpose. Instead you can
download the file in the background:

<URL:http://dotnet.mvps.org/dotnet/code/net/#InternetLoadFile>

Parsing an HTML file:

MSHTML Reference
<URL:http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/mshtml/reference/reference.asp>

- or -

..NET Html Agility Pack: How to use malformed HTML just like it was
well-formed XML...
<URL:http://blogs.msdn.com/smourier/archive/2003/06/04/8265.aspx>

Download:

<URL:http://www.codefluent.com/smourier/download/htmlagilitypack.zip>

- or -

SgmlReader 1.4
<URL:http://www.gotdotnet.com/Community/...mpleGuid=B90FDDCE-E60D-43F8-A5C4-C3BD760564BC>

If the file read is in XHTML format, you can use the classes contained in
the 'System.Xml' namespace for reading information from the file.
 
Maybe this code snippet is of use for you:

<URL:http://dotnet.mvps.org/dotnet/code/net/#CookieRequest>

Not sure what I'm doing wrong but I am having difficulties using WebClient.
No problems with HttpWebRequest but I am not sure how to access the DOM with
MSHtml...

This is what I'm trying with WebClient:

Dim Client As New WebClient()
Client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Client.DownloadData("https://website...")
Client.Headers.Add("Cookie", Client.ResponseHeaders("Set-Cookie"))
Dim Data() As Byte = Client.UploadData("https://website...", "POST",
Encoding.UTF8.GetBytes("postdatahere"))
Dim thisstring As String
thisstring = Encoding.UTF8.GetString(Data)
Client.Dispose()
MsgBox(Encoding.UTF8.GetString(Data))

It seems as if the cookie isn't being retained properly, here is what works
for me using HttpWebRequest:

Dim myWebRequest As HttpWebRequest =
CType(WebRequest.Create(https://website...), HttpWebRequest)
Dim WebResponse As HttpWebResponse
Dim srReader As StreamReader
Dim swWriter As StreamWriter
Dim strPostData As String
Dim strResponse As String
Dim myMSHTML As mshtml.HTMLDocument
Dim myDoc As mshtml.IHTMLDocument2
myWebRequest.CookieContainer = New CookieContainer
WebResponse = myWebRequest.GetResponse
strPostData = "POST data"
myWebRequest = CType(WebRequest.Create(https://website...), HttpWebRequest)
myWebRequest.Method = "POST"
myWebRequest.ContentLength = strPostData.length
myWebRequest.ContentType = "Content-Type: application/x-www-form-urlencoded"
myWebRequest.CookieContainer = New CookieContainer
swWriter = New StreamWriter(myWebRequest.GetRequestStream)
swWriter.Write(strPostData)
swWriter.Close()
WebResponse = myWebRequest.GetResponse
srReader = New StreamReader(WebResponse.GetResponseStream)
strResponse = srReader.ReadToEnd
srReader.Close()

here's where i start to run into problems with HttpWebRequest though:

myDoc = New mshtml.HTMLDocument
myDoc.clear()
myDoc.write(strResponse)
myDoc.close()
 
After experimenting some more I found that the following is enough to do the
trick:


strPostData = "POST Data"
myWebRequest = CType(WebRequest.Create(https://website...), HttpWebRequest)
myWebRequest.Method = "POST"
myWebRequest.ContentLength = strPostData.length
myWebRequest.ContentType = "Content-Type: application/x-www-form-urlencoded"
myWebRequest.CookieContainer = New CookieContainer

If the CookieContainer is commented out things do not work. I guess I need
to figure out how to duplicate this with WebClient (or convert this response
to a MSHtml.Document).
 
Back
Top