WebClien简单示例(一)

MSDN:WebClient提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法.

Demo(一):简单的请求请求一个页面内容

View Code
using System;
using System.IO;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String url = "http://www.baidu.com";
            
            //先设定代理(在使用代理进行上网的时候用,如果没有使用代理则无需设置)
            WebProxy proxy = new WebProxy("192.168.19.9", 80);                                      //创建 代理服务器设置对象 的实例
            proxy.BypassProxyOnLocal = false;                                                       //代理服务器需要验证
            proxy.Credentials = new NetworkCredential("jiaquanzhen", "abcd_123", "yellowpage");     //用户名密码
            GlobalProxySelection.Select = proxy;

            //开始请求
            WebClient client = new WebClient();
            //设置请求的资源种类
            client.Headers.Add("Accept", @"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, 
                                        application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            client.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
            //获取网上资源的stream
            System.IO.Stream stream = client.OpenRead(url);
            //解码获取内容
            System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);
            String str = reader.ReadToEnd();

            Console.WriteLine(str);
        }
    }
}

Demo(二):异步请求获取数据

View Code
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Net;
using System.Text;

namespace WebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnBegin.Click += new EventHandler(btnBegin_Click);
        }

        void btnBegin_Click(object sender, EventArgs e)
        {
            //请求的url
            Uri uri = new Uri("http://www.baidu.com");

            //初始化webclient,并设置头属性
            WebClient client = new WebClient();
            client.Headers.Add("Accept", @"image/gif,image/x-xbitmap,image/jpeg,application/x-shockwave-flash,
                                        appllication/vnd.ms-excel,application/vnd.ms-powerpoint,aplication/msword,*/*");
            client.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");

            //进行异步读取
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(uri);
        }

        //回调函数
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Stream stream = e.Result;
            StreamReader reader = new StreamReader(stream, Encoding.Default);
            String str = reader.ReadToEnd();
            Response.Write(str);
        }
    }
}

 

 

转载于:https://www.cnblogs.com/NaughtyBoy/archive/2012/06/21/2557657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值