当前位置:编程学习 > C#/ASP.NET >>

C# 采集器问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义正则表达式
            Regex reg = new Regex("http://t3.baidu.com/.+?.jpg");
            //Console.WriteLine("请输入要采集的路径");
            //string url = Console.ReadLine();

            //这是百度图片的页面
            string url = "http://image.baidu.com/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn=baiduimage&istype=2&fm=index&pv=&z=0&ie=utf-8&word=%E7%A8%8B%E5%BA%8F%E7%8C%BF";
            //获得页面中的源代码
            WebClient wc = new WebClient();
            byte [] buffe = wc.DownloadData(url);
            string html = Encoding.UTF8.GetString(buffe);

            //用正则表达式去匹配
            MatchCollection matchs = reg.Matches(html);
            foreach (Match match in matchs)
            {
                //如果匹配成功
                if (match.Success)
                {
                    //获得匹配的结果,也就是所有的图片地址
                    string imgUrl = match.Groups[0].Value;
                    //下载到本地
                    wc.DownloadFile(imgUrl, Path.Combine(@"G:\tmp", Path.GetFileName(imgUrl)));
                    //上面这一行报错: 远程服务器返回错误: (403) 已禁止。

                }
            }

            Console.ReadKey();
        }
    }
}


以上是我的C#代码。我拿上面匹配出来的路径在IE下打开试了下。有些可以正常看到图片。有些是403,  想知道要怎么判断远程服务器返回的是不是403 ? --------------------编程问答--------------------  怎么没人来回答?是不是看不懂意思还是怎么?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,