我们经常会对前台的html代码进行操作.用JS可以很简单的得到<html></html>里面的内容!我 这里主要利HttpWebRequest类与HttpWebResponse类来取得前台页面的全部代码(不包括<@%>内的代码!主要是利用HttpWebRequest类的GetResponse()返回一个HttpWebResponse内容,然后再通过HttpWebResponse类的GetResponseStream返回一个stream流,用streamReader类进行读取.我们可以很好的得到网站的前台内容,下面就是
完整代码:
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim html As String = gethtml("http://www.csdn.net")
Response.Write(html)
End Sub
Function gethtml(ByVal url As String) As String
Dim html As String = ""
Dim myRequest As HttpWebRequest
Dim myResponse As HttpWebResponse
Dim sr As StreamReader
Dim line As String
myRequest = CType(WebRequest.Create(url), HttpWebRequest)
myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
sr = New StreamReader(myResponse.GetResponseStream())
line = sr.ReadLine()
Do While Not line Is Nothing
line = HttpUtility.HtmlEncode(line) '转化字符
If line.Length <> 0 Then
html += line & "<br>"
End If
line = sr.ReadLine()
Loop
sr.Close()
Return html
End Function