Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace http_request
- {
- class HttpRequester
- {
- public HttpRequester()
- {
- ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
- }
- public IEnumerable<String> getImagesLink(Uri uri)
- {
- //Список ссылок на выход
- List<String> list = new List<string>();
- String response = getResponse(uri);
- //
- String condition = "<\\s*img\\s+src\\s?=\\s?\"(?:(?:(?<scheme>http[s]?|ftp[s]?)?(?::)?\\/\\/)?" +
- "(?<host>(?:[^:\\/\\s]+\\.[^:\\/\\s]+)|(?:localhost))(?::)?(?<port>\\d+)?)?(?:\\/)?(?<path>[^\"]+)\"";
- Regex regex = new Regex(condition, RegexOptions.IgnoreCase);
- MatchCollection matches = regex.Matches(response);
- UriBuilder parsedUri;
- foreach (Match match in matches)
- {
- parsedUri = new UriBuilder();
- parsedUri.Scheme = !String.IsNullOrEmpty(match.Groups["scheme"].Value) ? match.Groups["scheme"].Value : uri.Scheme;
- parsedUri.Host = !String.IsNullOrEmpty(match.Groups["host"].Value) ? match.Groups["host"].Value : uri.Host;
- parsedUri.Port = !String.IsNullOrEmpty(match.Groups["port"].Value) ? Convert.ToInt32(match.Groups["port"].Value) : uri.Port;
- parsedUri.Port = parsedUri.Port == 80 ? -1 : parsedUri.Port;
- parsedUri.Path = match.Groups["path"].Value;
- list.Add(parsedUri.ToString());
- }
- return list;
- }
- private String getResponse(Uri uri)
- {
- String response = String.Empty;
- try
- {
- HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
- HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
- using (StreamReader stream = new StreamReader(
- resp.GetResponseStream(), Encoding.UTF8))
- {
- response = stream.ReadToEnd();
- }
- }
- catch (WebException ex)
- {
- return null;
- }
- return response;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement