VB.NET的代码如下:
Dim req As HttpWebRequest = WebRequest.Create("http://rss.sina.com.cn/news/marquee/ddt.xml")
Dim res As HttpWebResponse = req.GetResponse()
Dim strm As StreamReader = New StreamReader(res.GetResponseStream(), Encoding.UTF8)
Dim sline As String
Sline = strm.ReadToEnd() '这就是网页代码[/color]
引用如下:
Imports System.Net
Imports System.Text
Imports System.IO
代码如下:
Dim buf(1000000) As Byte
Dim request As HttpWebRequest = WebRequest.Create("http://www.mcfsoft.com.cn")
Dim response As HttpWebResponse = request.GetResponse()
Dim resStream As Stream = response.GetResponseStream()
Dim count As Integer = resStream.Read(buf, 0, buf.Length)
Dim str As String = Encoding.Default.GetString(buf, 0, count) '这里的str就是你要的网页代码
resStream.Close()
文件下载源代码。同时可以用来显示网页源代码。应该没有编码问题。
把下面注释的符号去掉就可以断点续传了。不过此功能不稳定
Sub Down(ByVal StrFileName As String, ByVal StrUrl As String)
Dim lStartPos As Long = 0
Dim fs As System.IO.FileStream
' If System.IO.File.Exists(StrFileName) Then
' fs = System.IO.File.OpenWrite(StrFileName)
' lStartPos = fs.Length
' fs.Seek(lStartPos, System.IO.SeekOrigin.Current)
' Else
fs = New System.IO.FileStream(StrFileName, System.IO.FileMode.Create)
lStartPos = 0
' End If
Try
Dim request As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(StrUrl), System.Net.HttpWebRequest)
If lStartPos > 0 Then
request.AddRange(CType(lStartPos, Integer))
End If
Dim ns As System.IO.Stream = request.GetResponse.GetResponseStream
Dim nbytes(512) As Byte
Dim nReadSize As Integer = 0
nReadSize = ns.Read(nbytes, 0, 512)
While nReadSize > 0
fs.Write(nbytes, 0, nReadSize)
nReadSize = ns.Read(nbytes, 0, 512)
End While
fs.Close()
ns.Close()
Catch ex As Exception
fs.Close()
End Try
End Sub
|