none
VB NET 2012 怎么读取网页源码啊? RRS feed

  • 问题

  • 本人刚入门看了人家的代码用了结果不行痛苦中……求方法

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim MyClient As New System.Net.WebClient
    Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead(URL), System.Text.Encoding.Default)
    Debug.WriteLine(MyReader.ReadToEnd)

    End Sub

    以上运行时出现:"System.IO.IOException"类型的第一次机会异常在 mscorlib.dll 中发生
    本人WIN7 64位系统。


    2014年1月8日 10:56

答案

  • 您好!

    我创建了一个Project来读取Bing的源码,代码如下:

    Imports System.Net
    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim request As WebRequest = WebRequest.Create("http://www.bing.com")
            Dim response As WebResponse = request.GetResponse()
            Dim resStream As Stream = response.GetResponseStream()
            Dim sr As New StreamReader(resStream, System.Text.Encoding.[Default])
            Dim SourceCode As String = sr.ReadToEnd()
            resStream.Close()
            sr.Close()
    
            System.IO.File.WriteAllText("Bing.txt", SourceCode)
            Console.ReadKey()
        End Sub
    
    End Module

    返回结果截图:

    在我这边运行是OK

    >>以上运行时出现:"System.IO.IOException"类型的第一次机会异常在 mscorlib.dll 中发生. <<

    通常情况下,这个异常发生在文件找不到的情况,或者一个文件多个程序占用着。您可以check下您其他部分的代码,是否有多个程序同时访问一个文件的现象,或者将需要写入的文件关闭掉。

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月9日 8:41
    版主

全部回复

  • 您好,因为这个问题和VB有关,我帮你把该问题移到VB中文开发论坛,那里你会得到更好的支持和帮助。谢谢你的合作!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月9日 2:18
  • 您好!

    我创建了一个Project来读取Bing的源码,代码如下:

    Imports System.Net
    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim request As WebRequest = WebRequest.Create("http://www.bing.com")
            Dim response As WebResponse = request.GetResponse()
            Dim resStream As Stream = response.GetResponseStream()
            Dim sr As New StreamReader(resStream, System.Text.Encoding.[Default])
            Dim SourceCode As String = sr.ReadToEnd()
            resStream.Close()
            sr.Close()
    
            System.IO.File.WriteAllText("Bing.txt", SourceCode)
            Console.ReadKey()
        End Sub
    
    End Module

    返回结果截图:

    在我这边运行是OK

    >>以上运行时出现:"System.IO.IOException"类型的第一次机会异常在 mscorlib.dll 中发生. <<

    通常情况下,这个异常发生在文件找不到的情况,或者一个文件多个程序占用着。您可以check下您其他部分的代码,是否有多个程序同时访问一个文件的现象,或者将需要写入的文件关闭掉。

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月9日 8:41
    版主