积极答复者
请问:如何获取网页内容

问题
-
我在网上找到一个新浪股票数据接口的帖子,但是我不会用程序(vb.net)获取我想要的字符串,请指教;
另外,应该怎么判断电脑是否已经联网?
谢谢!
帖子网址:http://blog.sina.com.cn/s/blog_540f22560100ba2k.html
Work8862
答案
-
你好,
可以使用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.- 已标记为答案 Youen ZenModerator 2013年1月8日 9:07
- 取消答案标记 Work8862 2013年1月31日 3:40
- 已标记为答案 Work8862 2013年3月11日 4:34
-
- 已标记为答案 ThankfulHeartModerator 2013年3月11日 10:39
全部回复
-
你好,
可以使用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.- 已标记为答案 Youen ZenModerator 2013年1月8日 9:07
- 取消答案标记 Work8862 2013年1月31日 3:40
- 已标记为答案 Work8862 2013年3月11日 4:34
-
@另外,我想判断联网和读取网页内容分开,就是每次启动程序只检测一次电脑是否联网
My.Computer.Network.IsAvailable
-
-
- 已标记为答案 ThankfulHeartModerator 2013年3月11日 10:39