{
WebRequest request = WebRequest.Create("http://www.baidu.com/");
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
test.Text = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();
}
catch (Exception ex)
{
test.Text = ex.Message;
}
//抓取图片
try
{
WebRequest request = WebRequest.Create("http://portrait7.sinaimg.cn/1859998982/blog/180.jpg");
WebResponse response = request.GetResponse();
Stream reader = response.GetResponseStream();
FileStream writer = new FileStream("D:\\logo.jpg", FileMode.OpenOrCreate, FileAccess.Write);
byte[] buff = new byte[1000000];
int c = 0; //实际读取的字节数
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
writer.Write(buff, 0, c);
}
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
response.Close();
//test.Text = "保存成功!";
}
catch (Exception ex)
{
test.Text = ex.Message;
}