Skip to content

Commit ae344f9

Browse files
authored
Merge pull request #49 from lexwebb/master
Added basic WebProxy support
2 parents 31593f3 + fb3fa34 commit ae344f9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

RedditSharp/Interfaces/IWebAgent.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public interface IWebAgent
99
CookieContainer Cookies { get; set; }
1010
string AuthCookie { get; set; }
1111
string AccessToken { get; set; }
12+
bool UseProxy { get; set; }
13+
WebProxy Proxy { get; set; }
14+
HttpWebRequest InjectProxy(HttpWebRequest request);
1215
HttpWebRequest CreateRequest(string url, string method);
1316
HttpWebRequest CreateGet(string url);
1417
HttpWebRequest CreatePost(string url);

RedditSharp/WebAgent.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ public int RequestsThisBurst
9797
get { return _requestsThisBurst; }
9898
}
9999

100+
/// <summary>
101+
/// Whether or not to use a proxy when executing web requests
102+
/// </summary>
103+
public bool UseProxy { get; set; }
104+
105+
/// <summary>
106+
/// Proxy for executing web requests, will not be used unless <see cref="UseProxy"/> is true
107+
/// </summary>
108+
public WebProxy Proxy { get; set; }
109+
100110
static WebAgent()
101111
{
102112
UserAgent = "";
@@ -141,6 +151,12 @@ public virtual JToken CreateAndExecuteRequest(string url)
141151
public virtual JToken ExecuteRequest(HttpWebRequest request)
142152
{
143153
EnforceRateLimit();
154+
155+
if(UseProxy)
156+
{
157+
request.Proxy = Proxy;
158+
}
159+
144160
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
145161
var result = GetResponseString(response.GetResponseStream());
146162

@@ -253,6 +269,7 @@ public virtual HttpWebRequest CreateRequest(string url, string method)
253269
}
254270
request.Method = method;
255271
request.UserAgent = UserAgent + " - with RedditSharp by /u/meepster23";
272+
request = InjectProxy(request);
256273
return request;
257274
}
258275

@@ -278,6 +295,7 @@ protected virtual HttpWebRequest CreateRequest(Uri uri, string method)
278295
}
279296
request.Method = method;
280297
request.UserAgent = UserAgent + " - with RedditSharp by /u/meepster23";
298+
request = InjectProxy(request);
281299
return request;
282300
}
283301

@@ -382,5 +400,18 @@ private static bool IsOAuth()
382400
{
383401
return RootDomain == "oauth.reddit.com";
384402
}
403+
404+
/// <summary>
405+
/// Inject the web proxy <see cref="Proxy"/> into the provided request
406+
/// </summary>
407+
/// <param name="request">The request object to inject the proxy into</param>
408+
public virtual HttpWebRequest InjectProxy(HttpWebRequest request)
409+
{
410+
if (this.UseProxy)
411+
{
412+
request.Proxy = this.Proxy;
413+
}
414+
return request;
415+
}
385416
}
386417
}

0 commit comments

Comments
 (0)