Skip to content

Commit cd4ba49

Browse files
authored
Merge pull request #72 from goigle/master
Fixing System.InvalidCastException on Mono
2 parents 14419ea + ed779e3 commit cd4ba49

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

RedditSharp/WebAgent.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,24 @@ protected virtual void EnforceRateLimit()
256256
public virtual HttpWebRequest CreateRequest(string url, string method)
257257
{
258258
EnforceRateLimit();
259-
bool prependDomain = !Uri.IsWellFormedUriString(url, UriKind.Absolute);
259+
bool prependDomain;
260+
// IsWellFormedUriString returns true on Mono for some reason when using a string like "/api/me"
261+
// additionally, doing this fixes an InvalidCastException
262+
if (Type.GetType("Mono.Runtime") != null)
263+
prependDomain = !url.StartsWith("http://") && !url.StartsWith("https://");
264+
else
265+
prependDomain = !Uri.IsWellFormedUriString(url, UriKind.Absolute);
260266
HttpWebRequest request;
261267
if (prependDomain)
262268
request = (HttpWebRequest)WebRequest.Create(string.Format("{0}://{1}{2}", Protocol, RootDomain, url));
263269
else
264270
request = (HttpWebRequest)WebRequest.Create(url);
265271
request.CookieContainer = Cookies;
272+
if (Type.GetType("Mono.Runtime") != null)
273+
{
274+
var cookieHeader = Cookies.GetCookieHeader(new Uri("http://reddit.com"));
275+
request.Headers.Set("Cookie", cookieHeader);
276+
}
266277
if (IsOAuth() && request.Host.ToLower() == "oauth.reddit.com")// use OAuth
267278
{
268279
request.Headers.Set("Authorization", "bearer " + AccessToken);//Must be included in OAuth calls

0 commit comments

Comments
 (0)