VB.NET使用WebRequest和WebResponse获得网页源代码

<%@ Page Language="VB" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.HttpPostedFile" %>
<%@ Import Namespace="System.Web.UI.HtmlControls.HtmlInputFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Dim url As String
    
    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        url = "http://news.qq.com/a/20080402/000148.htm"
        Response.Write(GetHttp(url, "get"))
    End Sub

    Sub Page_Unload(ByVal Sender As Object, ByVal E As EventArgs)
    End Sub
    
    Function GetHttp(ByVal This_Url As String, ByVal PostType As String) As String
        'HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持',也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。
        Dim httpReq As System.Net.HttpWebRequest
        ' HttpWebResponse 类用于生成发送 HTTP 请求和接收 HTTP 响'应的 HTTP 独立客户端应用程序。
        Dim httpResp As System.Net.HttpWebResponse

        Try
            If PostType = "POST" Then
                Dim TmpUrl() As String = Split(This_Url, "?")
                Dim PostStr As String = TmpUrl(TmpUrl.Length - 1)
                Dim requestBytes As Byte() = System.Text.Encoding.Default.GetBytes(PostStr)

                httpReq = WebRequest.Create(TmpUrl(0))
                httpReq.Method = "POST"
                httpReq.ContentType = "application/x-www-form-urlencoded"
                httpReq.ContentLength = requestBytes.Length
                Dim requestStream As Stream = httpReq.GetRequestStream()
                requestStream.Write(requestBytes, 0, requestBytes.Length)
                requestStream.Close()
            
                Dim res As HttpWebResponse = httpReq.GetResponse()
                Dim sr As StreamReader = New StreamReader(res.GetResponseStream(), System.Text.Encoding.Default)
                Dim backstr As String = sr.ReadToEnd()
                GetHttp = backstr
                sr.Close()
                res.Close()
            Else
                'Dim httpURL As New System.Uri(This_Url)
                httpReq = CType(System.Net.WebRequest.Create(This_Url), System.Net.HttpWebRequest)
                httpReq.ContentType = "application/x-www-form-urlencoded"
                'httpReq.Headers.Add("Accept-Language", "zh-cn")
            
                httpReq.Method = "GET"
                httpResp = CType(httpReq.GetResponse(), System.Net.HttpWebResponse)
                '如是中文,要设置编码格式为"GB2312"。
                Dim reader As StreamReader = New StreamReader(httpResp.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
                Dim respHTML As String = reader.ReadToEnd()
                GetHttp = respHTML
                httpResp.Close()
            End If
        Catch e As Exception
            GetHttp = "error"
        End Try
    End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>VB.NET测试-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


评论: 0 | 引用: 0 | 查看次数: 7300
发表评论
登录后再发表评论!