Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created November 16, 2010 17:13
Show Gist options
  • Save anaisbetts/702092 to your computer and use it in GitHub Desktop.
Save anaisbetts/702092 to your computer and use it in GitHub Desktop.
IObservable<KeyValuePair<string, bool>> ValidateUriAsync(string Uri)
{
var request = WebRequest.Create(Uri);
// This gives us a "GetResponseAsync" function whose prototype is
// IObservable[WebResponse] GetResponseAsync() - just like the sync
// version, but the return value wrapped in IObservable.
var response_fetcher = Observable.FromAsyncPattern<WebResponse>(
request.BeginGetResponse, request.EndGetResponse);
// The clever part here is the Catch - if the response_fetcher IObservable
// ends with OnError, we'll instead act as if it didn't end and splice in
// a "completed successfully" IObservable who returns null and ends
return response_fetcher()
.Catch(Observable.Return<WebResponse>(null))
.Select(resp => new KeyValuePair<string, bool>(Uri, resp != null && resp.StatusCode == HttpStatusCode.OK));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment