コード例 #1
0
        public Crawler Fetch()
        {
            if (!IsExternalIPAddress(this.SourceUrl))
            {
                State = "INVALID_URL";
                return(this);
            }
            var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest;

            using (var response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
                    return(this);
                }
                if (response.ContentType.IndexOf("image") == -1)
                {
                    State = "Url is not an image";
                    return(this);
                }
                var fileName = PathFormatter.Format(Path.GetFileName(this.SourceUrl), Config.GetString("catcherPathFormat"));

                try
                {
                    var stream = response.GetResponseStream();
                    var reader = new BinaryReader(stream);
                    using (var ms = new MemoryStream())
                    {
                        byte[] buffer = new byte[4096];
                        int    count;
                        while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            ms.Write(buffer, 0, count);
                        }
                        ms.Seek(0, SeekOrigin.Begin);
                        _uploadFolder.CreateFile(fileName, ms);
                        ServerUrl = _uploadFolder.GetUrl(fileName);
                    }
                    State = "SUCCESS";
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
コード例 #2
0
        public override void Process()
        {
            try
            {
                Start = String.IsNullOrEmpty(Request["start"]) ? 0 : Convert.ToInt32(Request["start"]);
                Size  = String.IsNullOrEmpty(Request["size"])
                    ? Config.GetInt("imageManagerListSize")
                    : Convert.ToInt32(Request["size"]);
            }
            catch (FormatException)
            {
                State = ResultState.InvalidParam;
                WriteResult();
                return;
            }
            var buildingList = new List <String>();

            try
            {
                buildingList.AddRange(_uploadFolder.ListFiles(PathToList, SearchExtensions, Start, Size, true)
                                      .Select(x => _uploadFolder.GetUrl(x)));
                Total    = buildingList.Count;
                FileList = buildingList.OrderBy(x => x).Skip(Start).Take(Size).ToArray();
            }
            catch (UnauthorizedAccessException)
            {
                State = ResultState.AuthorizError;
            }
            catch (DirectoryNotFoundException)
            {
                State = ResultState.PathNotFound;
            }
            catch (IOException)
            {
                State = ResultState.IOError;
            }
            finally
            {
                WriteResult();
            }
        }