|
| 1 | +--- |
| 2 | +title: "Getting Started" |
| 3 | +section: "AngleSharp.Io" |
| 4 | +--- |
| 5 | +# Getting Started |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +AngleSharp.Io comes currently in two flavors: on Windows for .NET 4.6 and in general targetting .NET Standard 2.0 platforms. |
| 10 | + |
| 11 | +Most of the features of the library do not require .NET 4.6, which means you could create your own fork and modify it to work with previous versions of the .NET-Framework. |
| 12 | + |
| 13 | +You need to have AngleSharp installed already. This could be done via NuGet: |
| 14 | + |
| 15 | +```ps1 |
| 16 | +Install-Package AngleSharp |
| 17 | +``` |
| 18 | + |
| 19 | +## Getting AngleSharp.Io over NuGet |
| 20 | + |
| 21 | +The simplest way of integrating AngleSharp.Io to your project is by using NuGet. You can install AngleSharp.Io by opening the package manager console (PM) and typing in the following statement: |
| 22 | + |
| 23 | +```ps1 |
| 24 | +Install-Package AngleSharp.Io |
| 25 | +``` |
| 26 | + |
| 27 | +You can also use the graphical library package manager ("Manage NuGet Packages for Solution"). Searching for "AngleSharp.Io" in the official NuGet online feed will find this library. |
| 28 | + |
| 29 | +## Setting up AngleSharp.Io |
| 30 | + |
| 31 | +To use AngleSharp.Io you need to add it to your `Configuration` coming from AngleSharp itself. |
| 32 | + |
| 33 | +### Requesters |
| 34 | + |
| 35 | +If you just want to use *all* available requesters provided by AngleSharp.Io you can do the following: |
| 36 | + |
| 37 | +```cs |
| 38 | +var config = Configuration.Default |
| 39 | + .WithRequesters() // from AngleSharp.Io |
| 40 | + .WithDefaultLoader(); // from AngleSharp |
| 41 | +``` |
| 42 | + |
| 43 | +This will register all requesters. Alternatively, the requesters can be provided explicitly. They are located in the `AngleSharp.Io.Network` namespace and have names such as `DataRequester`. |
| 44 | + |
| 45 | +Requesters can make use of `HttpClientHandler` instances. Hence using it, e.g., with a proxy is as simple as the following snippet: |
| 46 | + |
| 47 | +```cs |
| 48 | +var handler = new HttpClientHandler |
| 49 | +{ |
| 50 | + Proxy = new WebProxy(myProxyHost, false), |
| 51 | + PreAuthenticate = true, |
| 52 | + UseDefaultCredentials = false, |
| 53 | +}; |
| 54 | + |
| 55 | +var config = Configuration.Default |
| 56 | + .WithRequesters(handler) // from AngleSharp.Io with a handler config |
| 57 | + .WithDefaultLoader(); |
| 58 | +``` |
| 59 | + |
| 60 | +Alternatively, if you don't want to add all possible requesters, you can also just add a single requester from AngleSharp.Io: |
| 61 | + |
| 62 | +```cs |
| 63 | +var config = Configuration.Default |
| 64 | + .With(new HttpClientRequester()) // only requester |
| 65 | + .WithDefaultLoader(); |
| 66 | +``` |
| 67 | + |
| 68 | +In the code above we now only have a single requester - the `HttpClientRequester` coming from AngleSharp.Io. If we have an `HttpClient` already used somewhere we can actually re-use it: |
| 69 | + |
| 70 | +```cs |
| 71 | +var config = Configuration.Default |
| 72 | + .With(new HttpClientRequester(myHttpClient)) // re-using the HttpClient instance |
| 73 | + .WithDefaultLoader(); |
| 74 | +``` |
| 75 | + |
| 76 | +### Cookies |
| 77 | + |
| 78 | +To get improved cookie support, e.g., do |
| 79 | + |
| 80 | +```cs |
| 81 | +var config = Configuration.Default |
| 82 | + .WithTemporaryCookies(); // Uses memory cookies |
| 83 | +``` |
| 84 | + |
| 85 | +or if you want to have persistent cookies you can use: |
| 86 | + |
| 87 | +```cs |
| 88 | +var syncPath = $"Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)\\anglesharp.cookies"; |
| 89 | +var config = Configuration.Default |
| 90 | + .WithPersistentCookies(syncPath); // Uses sync cookies against the given path |
| 91 | +``` |
| 92 | + |
| 93 | +Alternatively, the new overloads for the `WithCookies` extension method can be used. |
| 94 | + |
| 95 | +### Downloads |
| 96 | + |
| 97 | +AngleSharp.Io offers you the possibility of a simplified downloading experience. Just use `WithStandardDownload` to redirect resources to a callback. |
| 98 | + |
| 99 | +In the simplest case you can write: |
| 100 | + |
| 101 | +```cs |
| 102 | +var config = Configuration.Default |
| 103 | + .WithStandardDownload((fileName, content) => |
| 104 | + { |
| 105 | + // store fileName with the content stream ... |
| 106 | + }); |
| 107 | +``` |
| 108 | + |
| 109 | +Alternatively, use `WithDownload`, which allows you to distinguish also on the provided MIME type. |
0 commit comments