获取网页中指定的字符串信息

作者:互联网   出处:控件中国网   2014-11-05 19:22:07   阅读:1

获取网页中指定的字符串信息

private void button2_Click(object sender, EventArgs e)
         {
             // Create a request for the URL.  
             WebRequest request = WebRequest.Create("http://www.componentcn.com/");
             // If required by the server, set the credentials.
             request.Credentials = CredentialCache.DefaultCredentials;
             // Get the response.
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
             // Display the status.
             MessageBox.Show(response.StatusDescription);
             Console.WriteLine(response.StatusDescription);
             // Get the stream containing content returned by the server.
             Stream dataStream = response.GetResponseStream();
             // Open the stream using a StreamReader for easy access.
             StreamReader reader = new StreamReader(dataStream, Encoding.Default);
             // Read the content.
             string responseFromServer = reader.ReadToEnd();
             //截取数据
             int i = responseFromServer.IndexOf("京");
             string dataBid = responseFromServer.Substring(i, 12);
 
             // Display the content.
             MessageBox.Show(dataBid);
             Console.WriteLine(responseFromServer);
             // Cleanup the streams and the response.
             reader.Close();
             dataStream.Close();
             response.Close();
         }

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat