title: How to validate existence of OneDrive document via its Share link?
body: <p>My C#/UWP app has a section where users can enter links to OneDrive documents and other web resources as reference information. A user can click a button to test a link after they've entered it to make sure it launches as expected. I need to validate that the link target exists before launching the URI and raise an error if the link is not valid before attempting to launch the URI.</p>
  
  <p>It's straight-forward to validate web sites and non-OneDrive web-based docs by creating a HttpWebRequest using the URL and evaluating the response's status value. (See sample code below.)</p>
  
  <p>However, OneDrive document share links seem to have problems with this approach, returning a [405 Method Not Allowed] error. I'm guessing this is because OneDrive share links do lots of forwarding and redirection before they get to the actual document.</p>
  
  <pre class="lang-cs prettyprint-override"><code>try
  {
      // Create the HTTP request
      HttpWebRequest request = WebRequest.Create(urlString) as HttpWebRequest;
      request.Timeout = 5000;    // Set the timeout to 5 seconds -- don't keep the user waiting too long!
      request.Method  = "HEAD";  // Get only the header information -- no need to download page content
  
  
  
      // Get the HTTP response
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
      {
          // Get the response status code
          int statusCode = (int)response.StatusCode;
  
          // Successful request...return true
          if (statusCode &gt;= 100 &amp;&amp; statusCode &lt; 400)
          {
              return true;
          }
  
          // Unsuccessful request (server-side errors)...return false
          else // if (statusCode &gt;= 500 &amp;&amp; statusCode &lt; 600)
          {
              Log.Error( $"URL not valid. Server error. (Status = {statusCode})" );
              return false;
          }
      }
  }
  
  
  // Handle HTTP exceptions
  catch (WebException e)
  {
      // Get the entire HTTP response from the exception
      HttpWebResponse response = (HttpWebResponse)e.Response;
  
  
      // Grab the HTTP status code from the response
      int statusCode = (int)response.StatusCode;
  
  
      // Unsuccessful request (client-side errors)...return false
      if (statusCode &gt;= 400 &amp;&amp; statusCode &lt;= 499)
      {
          Log.Error( $"URL not valid. Client error. (Status = {statusCode})" );
          return false;
      }
      else // Unhandled errors
      {
          Log.Error( $"Unhandled status returned for URL. (Status = {e.Status})" );
          return false;
      }
  }
  
  
  // Handle non-HTTP exceptions
  catch (Exception e)
  {
      Log.Error( $"Unexpected error. Could not validate URL." );
      return false;
  }
  </code></pre>
  
  <p>I can trap the 405 error and launch the URL anyhow using the <code>Windows.System.Launcher.LaunchUriAsync</code> method. The OneDrive link launches just fine...IF the OneDrive document actually exists.</p>
  
  <p>But if the document doesn't exist, or if the share permissions have been revoked, I end up with a browser page with something like a [404 Not Found] error...exactly what I'm trying to avoid by doing the validation!</p>
  
  <p><em><strong>Is there a way to validate OneDrive share links WITHOUT actually launching them in a browser?  Are there other types of links (bit.ly links, perhaps?) that also create problems in the same way? Perhaps a better question: Can I validate ALL web resources in the same way without knowing anything but the URL?</strong></em></p>