Hi,
I have a function in my WinForms app that needs to retrieve the user's default user agent. To do this, I've created a web page (ASP.NET) that displays the user agent in a Label in the Page_Load. The problem that I'm having is that when I fetch the HTML from that page, the result includes all of the HTML except what should be displayed in the Label (i.e., the user agent).
Here's my code:
When that code is run, it returns the following result:
However, when viewing the page's source in a browser, it contains the following code:
Any ideas why GetString is not picking up the Label contents?
Note: For reasons beyond my control, this app is coded using .NET 1.1. Otherwise, I'd be doing things a bit differently.
I have a function in my WinForms app that needs to retrieve the user's default user agent. To do this, I've created a web page (ASP.NET) that displays the user agent in a Label in the Page_Load. The problem that I'm having is that when I fetch the HTML from that page, the result includes all of the HTML except what should be displayed in the Label (i.e., the user agent).
Here's my code:
VB.NET:
Private Function GetUserAgent(ByVal theUrl As String) As String
Dim proc As New Process
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo.FileName = theUrl
proc.Start()
Dim client As New WebClient
Dim dataBuffer As Byte() = client.DownloadData(theUrl)
Dim result As String = Encoding.UTF8.GetString(dataBuffer)
'The following lines are commented out b/c when I first tested the
'application, no result was returned. Then I commented out these
'lines and discovered that while the HTML was being returned,
'the inner text of the <span> tag was blank.
'Dim searchStringBegin As String = "<span id=""Label1"">"
'Dim searchStringEnd As String = "</span>"
'Dim startIndex As Integer = result.IndexOf(searchStringBegin, 0)
'Dim endIndex As Integer = result.IndexOf(searchStringEnd, startIndex)
'Dim len As Integer = endIndex - startIndex - searchStringBegin.Length
'Return result.Substring(startIndex + searchStringBegin.Length, len)
Return result
End Function
When that code is run, it returns the following result:
VB.NET:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>
</head>
<body>
<form name="form1" method="post" action="ua.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIOTEyNDUzNDYPZBYCAgMPZBYCAgEPFgIeBFRleHRlZGRFwgcdsZHQ3wZjwcDujr/taZqCZg==" />
<p>Your user agent is: </p>
</form>
</body>
</html>
However, when viewing the page's source in a browser, it contains the following code:
VB.NET:
<p>Your user agent is: <span id="Label1">(user agent string here)</span></p>
Any ideas why GetString is not picking up the Label contents?
Note: For reasons beyond my control, this app is coded using .NET 1.1. Otherwise, I'd be doing things a bit differently.