C# C/S程序请求网页地址,并获取返回数据代码

来源:互联网 发布:深圳婚纱摄影 知乎 编辑:程序博客网 时间:2024/06/06 14:58


  C# C/S程序请求网页地址,并获取返回数据代码
           
            string url = "http://www.baidu.com/";

            System.Net.HttpWebRequest webrequest =     (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
           
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();//请求连接,并反回数据

            Stream stream = webresponse.GetResponseStream();//把返回数据转换成流文件
           
            byte[] rsByte = new Byte[webresponse.ContentLength];  //把流文件转换为字节数组
           
            try
            {
                stream.Read(rsByte, 0, (int)webresponse.ContentLength);
                string HTML=System.Text.Encoding.Default.GetString(rsByte, 0, rsByte.Length).ToString();
            }
            catch (Exception exp)
            {
                exp.ToString();
            }

原创粉丝点击