不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
VB.NET使用WebRequest和WebResponse获得网页源代码
编辑:dnawo 日期:2008-04-02
复制内容到剪贴板
程序代码

<%@ 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>
<%@ 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
发表评论
请登录后再发表评论!