博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebClien简单示例(一)
阅读量:7110 次
发布时间:2019-06-28

本文共 2851 字,大约阅读时间需要 9 分钟。

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

你可能感兴趣的文章
三星推CHG90显示器,高知新富与土豪谁为之疯狂?
查看>>
Processing XML with Java
查看>>
一个不错的开源wiki
查看>>
(转)如何增加SignalTap II能觀察的reg與wire數量? (SOC) (Quartus II) (SignalTap II)
查看>>
各种数据库简单连接
查看>>
程序员的运动建议(转载)
查看>>
SQL Server Service Broker 基础知识 PPT 文档 – 分享下载!
查看>>
mysql-python安装时EnvironmentError: mysql_config not found
查看>>
3个著名加密算法(MD5、RSA、DES)的解析
查看>>
免费资源:Polaris UI套件 + Linecons图标集(AI, PDF, PNG, PSD, SVG)
查看>>
纯净版CentOS64位安装LAMP的时候出现的问题总结
查看>>
Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
查看>>
Heredoc技术<<<
查看>>
(转载) eclipse 找不到Courier New 样式
查看>>
大丈夫不可一日无权啊!——项目经理的误区(2)(转)
查看>>
HDU2309:ICPC Score Totalizer Software
查看>>
Nginx源码分析-定时器的实现及使用
查看>>
JAVA的StringBuffer类
查看>>
搜索账号建设内网wiki
查看>>
XtremSF和XtremSW简介
查看>>