Skip to content

Commit

Permalink
The WebClient no longer EXPLODES if a 404 or 500 (etc) status code is…
Browse files Browse the repository at this point in the history
… returned
  • Loading branch information
remi committed Feb 25, 2011
1 parent 0080997 commit 549d9e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
38 changes: 32 additions & 6 deletions Mara.Drivers.WebClient/WebClient.cs
Expand Up @@ -94,7 +94,12 @@ public class CookieAwareWebClient : System.Net.WebClient {
set { _client = value; }
}

string AppHost { get { return Mara.AppHost; }}
string _appHost;

public virtual string AppHost {
get { return _appHost ?? Mara.AppHost; }
set { _appHost = value; }
}

public void ResetSession() {
_client = new CookieAwareWebClient();
Expand All @@ -103,11 +108,32 @@ public class CookieAwareWebClient : System.Net.WebClient {
public string Body { get; set; }

public void Visit(string path) {
var url = (path.StartsWith("/")) ? (Mara.AppHost + path) : path;

Body = Client.DownloadString(url);
Doc = null;
_currentUrl = url;
var url = (path.StartsWith("/")) ? (AppHost + path) : path;

try {
Body = Client.DownloadString(url);
Doc = null;
_currentUrl = url;
} catch (WebException ex) {
// Incase something blows up below here, be sure to always print out the url
Console.WriteLine("WebException thrown when trying to request {0}.\n{1}", url, ex);
_currentUrl = url;

var response = ex.Response as HttpWebResponse;
if (response == null)
throw new Exception(string.Format("WebException thrown when trying to get {0}. {1}", url, ex));

var statusCode = (int) response.StatusCode;

// Read the body of the response
Body = null;
using (var reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
Body = reader.ReadToEnd();

} catch (Exception ex) {
Console.WriteLine("Unexpected exception thrown while trying to Visit {0}", url);
throw ex;
}
}

// also initialize forms here
Expand Down
5 changes: 1 addition & 4 deletions Mara/MaraInstance.cs
Expand Up @@ -47,10 +47,7 @@ public partial class Mara : IDriver { // TODO ... inherit from Mara.Driver? May
if (_page == null) _page = Mara.Driver;
return _page;
}
set {
if (_page != null) _page.Close();
_page = value;
}
set { _page = value; }
}

public string Body { get { return Page.Body; }}
Expand Down

0 comments on commit 549d9e4

Please sign in to comment.