none
请问:如何获取网页内容 RRS feed

答案

  • 你好,

    可以使用WebRequest来实现,这篇MSDN文章提供了一套完整代码,How to: Send Data Using the WebRequest Class。然后使用String.Split就可以得到你想要的数据了。至于判断是否已经联网,我想也可以使用下面的代码,如果有回复,则可判断为已经联网。

    Imports System.Net
    Imports System.Text
    Imports System.IO
    
    '....
    
            ' Create a request using a URL that can receive a post. 
            Dim request As WebRequest = WebRequest.Create("http://hq.sinajs.cn/list=sh601006")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
            ' Create POST data and convert it to a byte array.
            Dim postData As String = "This is a test that posts this string to a Web server."
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()
      
    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月2日 3:15
    版主
  • ProgrammingVolunteer你好,问题已经解决,我就是不要频繁测试网络连接就可以了,谢谢你

    Work8862

    2013年3月11日 4:36

全部回复

  • 你好,

    可以使用WebRequest来实现,这篇MSDN文章提供了一套完整代码,How to: Send Data Using the WebRequest Class。然后使用String.Split就可以得到你想要的数据了。至于判断是否已经联网,我想也可以使用下面的代码,如果有回复,则可判断为已经联网。

    Imports System.Net
    Imports System.Text
    Imports System.IO
    
    '....
    
            ' Create a request using a URL that can receive a post. 
            Dim request As WebRequest = WebRequest.Create("http://hq.sinajs.cn/list=sh601006")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
            ' Create POST data and convert it to a byte array.
            Dim postData As String = "This is a test that posts this string to a Web server."
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()
      
    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月2日 3:15
    版主
  • 版主的代码我试过,可以用

    因为股票数量比较多,我用循环读取的时候就不行了不知道怎么回事?

    另外,我想判断联网和读取网页内容分开,就是每次启动程序只检测一次电脑是否联网

    就可以了,

    应该怎么办呢?


    Work8862


    • 已编辑 Work8862 2013年1月31日 3:41
    2013年1月31日 3:39
  • @另外,我想判断联网和读取网页内容分开,就是每次启动程序只检测一次电脑是否联网

    My.Computer.Network.IsAvailable


    帮助一起改进论坛质量?提交你的意见于此。
    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    2013年2月4日 12:42
    版主
  • @因为股票数量比较多,我用循环读取的时候就不行了不知道怎么回事?

    你怎么做的?


    帮助一起改进论坛质量?提交你的意见于此。
    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    2013年2月4日 12:43
    版主
  • ProgrammingVolunteer你好,问题已经解决,我就是不要频繁测试网络连接就可以了,谢谢你

    Work8862

    2013年3月11日 4:36