欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 获取网页中指定的字符串信息的实例代码

程序员文章站 2023-12-15 18:37:40
复制代码 代码如下:       private void button2_click(object sende...
复制代码 代码如下:

       private void button2_click(object sender, eventargs e)
          {
              // create a request for the url.  
              webrequest request = webrequest.create("http://www.baidu.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();
         }

上一篇:

下一篇: