新建新的空网站和一个default.aspx页面测试,实验例子:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HttpWebRequest web = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); //创建一个请求 web.Method = "GET"; //方式为get web.ContentType = "text/html;charset=UTF-8"; //定义获取数据的类型 HttpWebResponse response = (HttpWebResponse)web.GetResponse(); //获取响应 string result = ""; using (Stream stream = response.GetResponseStream()) //定义缓存,将数据读取出来 { StreamReader sr = new StreamReader(stream); result = sr.ReadToEnd(); } Label1.Text = result.ToString(); } }
时间: 2024-10-06 20:58:33