Exemplo n.º 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.GetResponseAsync().Result 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);
                }
                ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), Config.GetString("catcherPathFormat"), _userid);
                if (!ServerUrl.StartsWith("/upFiles"))
                {
                    State = "Url is invalid!";
                    return(this);
                }
                var savePath = _context.MapPath(ServerUrl);
                var folder   = Path.GetDirectoryName(savePath);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                try
                {
                    var    stream = response.GetResponseStream();
                    var    reader = new BinaryReader(stream);
                    byte[] bytes;
                    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);
                        }
                        bytes = ms.ToArray();
                    }
                    File.WriteAllBytes(savePath, bytes);
                    State = "SUCCESS";
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
Exemplo n.º 2
0
        private bool CanLogin(object parameter)
        {
            bool validServerUrl =
                !string.IsNullOrWhiteSpace(ServerUrl) &&
                Uri.IsWellFormedUriString(ServerUrl, UriKind.Absolute) &&
                ServerUrl.StartsWith("http", StringComparison.InvariantCultureIgnoreCase);

            return
                (validServerUrl &&
                 !string.IsNullOrWhiteSpace(Username) &&
                 !string.IsNullOrWhiteSpace(_getPasswordDelegate?.Invoke()));
        }