Skip to content

Commit 6b56bfd

Browse files
committed
Core - Add IRequestContext.SetProxy
Add helper method for setting proxy on RequestContext Issue #3235
1 parent e149cab commit 6b56bfd

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

CefSharp/RequestContextExtensions.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5+
using System;
6+
using System.Collections.Generic;
57
using System.IO;
68
using System.Threading.Tasks;
79

@@ -47,6 +49,62 @@ public static void LoadExtensionsFromDirectory(this IRequestContext requestConte
4749
}
4850
}
4951

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+
50108
/// <summary>
51109
/// Clears all HTTP authentication credentials that were added as part of handling
52110
/// <see cref="IRequestHandler.GetAuthCredentials(IWebBrowser, IBrowser, string, bool, string, int, string, string, IAuthCallback)"/>.

0 commit comments

Comments
 (0)