我們經常會對前台的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