C#爬虫基本知识

url编码解码
首先引用程序集System.Web.dll
在这里插入图片描述
如果要解码某个url的参数值的话,可以调用下面的方法:
System.Web.HttpUtility.UrlDecode(string)
对某个url参数进行编码:
string s = “[1,2]”; string result = System.Web.HttpUtility.UrlEncode(s);

HttpWebRequest HttpWebResponse的使用
string url = “www.baidu.com”;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
// request.Accept = …(根据实际情况填写)
// request.Method = …(根据实际情况填写)
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

using(Stream s = response.GetResponseStream())
{
using(StreamReader reader = new StreamReader(s))
{
string data = reader.ReadToEnd();
}
s.Close();
}

response.Close();
要注意Stream 和 HttpWebResponse都实现了IDisposeable接口,所以要用using语句包裹,或者自行调用其Dispose()方法.还有,他们两在使用完后有调用一下他们的Close()方法来关闭连接.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值