|
2 | 2 | //
|
3 | 3 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
5 | 7 | using System.IO;
|
6 | 8 | using System.Threading.Tasks;
|
7 | 9 |
|
@@ -47,6 +49,62 @@ public static void LoadExtensionsFromDirectory(this IRequestContext requestConte
|
47 | 49 | }
|
48 | 50 | }
|
49 | 51 |
|
| 52 | + /// <summary> |
| 53 | + /// Sets the proxy server for the specified <see cref="IRequestContext"/> |
| 54 | + /// MUST be called on the CEF UI Thread |
| 55 | + /// </summary> |
| 56 | + /// <param name="requestContext">request context</param> |
| 57 | + /// <param name="scheme">is the protocol of the proxy server, and is one of: 'http', 'socks', 'socks4', 'socks5'. Also note that 'socks' is equivalent to 'socks5'.</param> |
| 58 | + /// <param name="host">host</param> |
| 59 | + /// <param name="port">post</param> |
| 60 | + /// <param name="errorMessage">error message</param> |
| 61 | + /// <returns>returns true if successfull, false otherwise.</returns> |
| 62 | + /// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with |
| 63 | + /// preference 'proxy' and mode of 'fixed_servers'</remarks> |
| 64 | + public static bool SetProxy(this IRequestContext requestContext, string scheme, string host, string port, out string errorMessage) |
| 65 | + { |
| 66 | + if (string.IsNullOrWhiteSpace(host)) |
| 67 | + { |
| 68 | + throw new ArgumentException("Cannot be null or empty", "host"); |
| 69 | + } |
| 70 | + |
| 71 | + if (string.IsNullOrWhiteSpace(port)) |
| 72 | + { |
| 73 | + throw new ArgumentException("Cannot be null or empty", port); |
| 74 | + } |
| 75 | + |
| 76 | + //Default to using http scheme if non provided |
| 77 | + if (string.IsNullOrWhiteSpace(scheme)) |
| 78 | + { |
| 79 | + scheme = "http"; |
| 80 | + } |
| 81 | + |
| 82 | + var v = new Dictionary<string, object> |
| 83 | + { |
| 84 | + ["mode"] = "fixed_servers", |
| 85 | + ["server"] = scheme + "://" + host + ":" + port |
| 86 | + }; |
| 87 | + |
| 88 | + return requestContext.SetPreference("proxy", v, out errorMessage); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Sets the proxy server for the specified <see cref="IRequestContext"/>. |
| 93 | + /// Protocol for the proxy server is http |
| 94 | + /// MUST be called on the CEF UI Thread |
| 95 | + /// </summary> |
| 96 | + /// <param name="requestContext">request context</param> |
| 97 | + /// <param name="host">host</param> |
| 98 | + /// <param name="port">post</param> |
| 99 | + /// <param name="errorMessage">error message</param> |
| 100 | + /// <returns>returns true if successfull, false otherwise.</returns> |
| 101 | + /// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with |
| 102 | + /// preference 'proxy' and mode of 'fixed_servers'</remarks> |
| 103 | + public static bool SetProxy(this IRequestContext requestContext, string host, string port, out string errorMessage) |
| 104 | + { |
| 105 | + return requestContext.SetProxy(null, host, port, out errorMessage); |
| 106 | + } |
| 107 | + |
50 | 108 | /// <summary>
|
51 | 109 | /// Clears all HTTP authentication credentials that were added as part of handling
|
52 | 110 | /// <see cref="IRequestHandler.GetAuthCredentials(IWebBrowser, IBrowser, string, bool, string, int, string, string, IAuthCallback)"/>.
|
|
0 commit comments