Asp获取⽹页源代码
测试环境:VS2008+VB
1Public Shared Function GetWebContent(ByVal sURL As String) As String
2Try
3Dim request As Net.WebRequest
4Dim response As Net.WebResponse
5            request = Net.WebRequest.Create(sURL)
6            response = request.GetResponse()
7Dim reader As New System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")) //读取的编码
8Dim s As String = reader.ReadToEnd
9            reader.Close()
10            reader.Dispose()
11            response.Close()
12Return s
13Catch ex As Exception
14Return""
15End Try
16End Function
提交数据(Post)并获取返回值
1Public Shared Function PostData(ByVal sUrl As String, ByVal sData As String) As String
2Try
3Dim Encoding As New ASCIIEncoding
4Dim bData As Byte() = Encoding.GetBytes(sData)
5Dim Request As Net.WebRequest = Net.WebRequest.Create(sUrl)
6            Request.Method = "POST"
7            Request.ContentType = "application/x-www-form-urlencoded"
8            Request.ContentLength = bData.Length
9Dim newStream As System.IO.Stream = Request.GetRequestStream()
10            newStream.Write(bData, 0, sData.Length)
11            newStream.Close()
12Dim Response As Net.WebResponse = Request.GetResponse
13Dim Reader As New System.IO.StreamReader(Response.GetResponseStream(), Text.Encoding.GetEncoding("utf-8"))
14Dim content As String = Reader.ReadToEnd
asp资讯网站源码
15            Reader.Close()
16            Reader.Dispose()
17            Response.Close()
18Return content
19Catch ex As Exception
20Return""
21End Try
22End Function
只是需要注意⼀点,如果提交的参数中包含中⽂,需要进⾏编码。提交的Url需要使⽤绝对路径,开发环境中可以使⽤1Dim sUrl As String="⽹址" //必须是绝对路径。如/提交的页⾯
2Dim sData As String
3 sData="try="+System.Web.HttpUtility.UrlEncode("测试")+"&try2="+System.Web.HttpUtility.UrlEncode("测试2")
4 Response.Write(PostData(sUrl,sData))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。