Пример #1
0
    /// <summary>
    /// Initializes a new instance of the <see cref="Reader" /> class.
    /// </summary>
    /// <param name="options">The HTTP options.</param>
    public Reader(HttpOptions options = null)
    {
      // initialize transcoder
      _transcoder = new NReadabilityTranscoder(
        dontStripUnlikelys: false,
        dontNormalizeSpacesInTextContent: true,
        dontWeightClasses: false,
        readingStyle: ReadingStyle.Ebook,
        readingMargin: ReadingMargin.Narrow,
        readingSize: ReadingSize.Medium
      );

      // get default HTTP options if none available
      if (options == null)
      {
        options = HttpOptions.CreateDefault();
      }

      _options = options;

      // initialize custom encoder
      _encoder = new Encodings.Encoder(true);

      // initialize HTTP client
      _httpClient = new HttpClient(options.CustomHttpHandler ?? new HttpClientHandler()
      {
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
        AllowAutoRedirect = true
      });

      if (options.RequestTimeout.HasValue)
      {
        _httpClient.Timeout = TimeSpan.FromSeconds(options.RequestTimeout.Value);
      }

      // add accept types
      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

      // add accepted encodings
      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip,deflate");

      // add user agent
      string userAgent = options.UseMobileUserAgent ? options.UserAgentMobile : options.UserAgent;

      string version = typeof(Reader).GetTypeInfo().Assembly.FullName.Split(',')[1].Split('=')[1];

      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", String.Format(userAgent, "; ReadSharp/" + version));
    }
Пример #2
0
    /// <summary>
    /// Initializes a new instance of the <see cref="Reader" /> class.
    /// </summary>
    /// <param name="options">The HTTP options.</param>
    public Reader(HttpOptions options = null, TranscoderOptions transcoderOptions = null)
    {      
        // get default HTTP options if none available
      if (options == null)
      {
        options = HttpOptions.CreateDefault();
      }

        if (transcoderOptions == null)
        {
            transcoderOptions = new TranscoderOptions();
        }

      _options = options;

      _transcoder = CreateTranscoder(transcoderOptions);

      // initialize custom encoder
      _encoder = new Encodings.Encoder(true);

      // initialize HTTP client
      _httpClient = new HttpClient(options.CustomHttpHandler ?? new HttpClientHandler()
      {
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
        AllowAutoRedirect = true
      });

      if (options.RequestTimeout.HasValue)
      {
        _httpClient.Timeout = TimeSpan.FromSeconds(options.RequestTimeout.Value);
      }

      // add accept types
      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

      // add accepted encodings
      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip,deflate");

      // add user agent
      string userAgent = options.UseMobileUserAgent ? options.UserAgentMobile : options.UserAgent;

      string version = typeof(Reader).GetTypeInfo().Assembly.FullName.Split(',')[1].Split('=')[1];

      _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", String.Format(userAgent, "; ReadSharp/" + version));
    }