以下代码演示 访问InternetWebClient的用法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WebApp2
{
/// <summary>
/// 访问Internet--WebClient的用法
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string url=txtURL.Text;
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("请输入你要加载的网址");
return;
}
if(!url.StartsWith("http://",StringComparison.CurrentCultureIgnoreCase))
{
url = "http://" + url;
}
WebClient client = new WebClient();
Stream strm = client.OpenRead(url);
StreamReader sr = new StreamReader(strm);
string line;
while ((line = sr.ReadLine()) != null)
{
//填充到listBox里
lstWeb.Items.Add(line);
}
strm.Close();
}
}
}