Advertisement
AlukardBF

lala

Feb 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace http_request
  10. {
  11.     class HttpRequester
  12.     {
  13.         public HttpRequester()
  14.         {
  15.             ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
  16.         }
  17.         public IEnumerable<String> getImagesLink(Uri uri)
  18.         {
  19.             //Список ссылок на выход
  20.             List<String> list = new List<string>();
  21.             String response = getResponse(uri);
  22.             //
  23.             String condition = "<\\s*img\\s+src\\s?=\\s?\"(?:(?:(?<scheme>http[s]?|ftp[s]?)?(?::)?\\/\\/)?" +
  24.                 "(?<host>(?:[^:\\/\\s]+\\.[^:\\/\\s]+)|(?:localhost))(?::)?(?<port>\\d+)?)?(?:\\/)?(?<path>[^\"]+)\"";
  25.             Regex regex = new Regex(condition, RegexOptions.IgnoreCase);
  26.             MatchCollection matches = regex.Matches(response);
  27.             UriBuilder parsedUri;
  28.             foreach (Match match in matches)
  29.             {
  30.                 parsedUri = new UriBuilder();
  31.                 parsedUri.Scheme = !String.IsNullOrEmpty(match.Groups["scheme"].Value) ? match.Groups["scheme"].Value : uri.Scheme;
  32.                 parsedUri.Host = !String.IsNullOrEmpty(match.Groups["host"].Value) ? match.Groups["host"].Value : uri.Host;
  33.                 parsedUri.Port = !String.IsNullOrEmpty(match.Groups["port"].Value) ? Convert.ToInt32(match.Groups["port"].Value) : uri.Port;
  34.                 parsedUri.Port = parsedUri.Port == 80 ? -1 : parsedUri.Port;
  35.                 parsedUri.Path = match.Groups["path"].Value;
  36.                 list.Add(parsedUri.ToString());
  37.             }
  38.             return list;
  39.         }
  40.         private String getResponse(Uri uri)
  41.         {
  42.             String response = String.Empty;
  43.             try
  44.             {
  45.                 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
  46.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  47.                 using (StreamReader stream = new StreamReader(
  48.                      resp.GetResponseStream(), Encoding.UTF8))
  49.                 {
  50.                     response = stream.ReadToEnd();
  51.                 }
  52.             }
  53.             catch (WebException ex)
  54.             {
  55.                 return null;
  56.             }
  57.             return response;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement