• 0

[WPF] Alternative to the WebBrowser control?


Question

3 answers to this question

Recommended Posts

  • 0

Are you looking to display the web content direct to the user, or to process the returned HTML response within your own code? If so I've used Net.HttpWebRequest to some good effect, including navigating a website, posting data and obtaining the response data I needed. If you're interested I could post some code.

  • 0
  sathenzar said:
yeah pretty much do the same exact thing like be able to retrieve the html page, manipulate it and be able to make calls between the app and the web page.

Ok well here is a bit of code from an app I'm developing right now that communicates via http/html with another website to retrieve data, it includes logging on via a form based logon system. Its not totally finished, its error checking is quite poor.... but the main thing is it works and you can build on it. Let me know how you get on, and if you find it useful.

Mike

Try

' Set the initial parameters

Dim domain As String = "https://the.website.com"

Dim encoding As New System.Text.ASCIIEncoding

Dim CookieC As New Net.CookieContainer

Dim DataStart As Integer

Dim DataTemp As Integer

' Use the appropriate HTML field names to stuff into the post header

Dim PostData As String = _

"USER=" & Username & _

"&PASSWORD=" & Password & _

"&TARGET=/warranty/ExtendedWarrantyAction.action" & _

"&SMLOCALE=GB-EN" & _

"&SMAUTHREASON=0" & _

"&SiteMinderLogin=yes"

Dim Data() As Byte = encoding.GetBytes(PostData)

' Initialise the request

Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain & "/login.fcc")

With LoginReq

.KeepAlive = False

.Method = "POST"

' Note: if the page uses a redirect if will fail

.AllowAutoRedirect = False

.ContentType = "application/x-www-form-urlencoded"

.ContentLength = Data.Length

' Set empty container

.CookieContainer = CookieC

End With

' Add the POST data

Dim SendReq As IO.Stream = LoginReq.GetRequestStream

SendReq.Write(Data, 0, Data.Length)

SendReq.Close()

' Obtain the response

Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()

' Retreive the headers from the request (e.g. the location header)

' Add any returned cookies to the cookie collection

CookieC.Add(LoginRes.Cookies)

PostData = "p_sn=" & IMEI & "&formPart=wave"

Dim SNData() As Byte = encoding.GetBytes(PostData)

LoginReq = Net.WebRequest.Create(domain & "/warrantycheck.action")

With LoginReq

.KeepAlive = False

.Method = "POST"

.ContentType = "application/x-www-form-urlencoded"

.ContentLength = SNData.Length

.AllowAutoRedirect = True

.CookieContainer = CookieC

End With

' Add the POST data

SendReq = LoginReq.GetRequestStream

SendReq.Write(SNData, 0, SNData.Length)

SendReq.Close()

LoginRes = LoginReq.GetResponse()

Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)

' Locate the warranty data

Dim HTML As String = sReader.ReadToEnd

If InStr(HTML, "not found") Then

Result = -6

Exit Sub

End If

Result = 1

Catch

Result = -7

End Try

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.