.NET Daily

Microsoft ASP.NET

How to Get External URL Reference in ASP.NET

Posted on .

How to Get External URL Reference in ASP.NET


We recently had a requirement to track the initial location of our users in a Single Sign On environment.

One essential information that was needed for this requirement was to get the previous external URL from which the user came in our website.

The external URL can be very easy extracted with the following code:

if (Request.ServerVariables["HTTP_REFERER"] != null)

NOTE: If the users type the URL directly into the navigation bar of the browser, the “HTTP_REFERER” will be null regardless of the website that they currently have loaded. If fact the above code returns the external URL only in the case of a redirect from the source (hyperlink or response.redirect method).

One more thing, I also have discovered a possibility to extract markup from a page by specifying it’s URL. Below you have code to extract the title of a page by specifying it’s URL:

/// <summary>
/// Get Page Title by URL
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string GetPage(string url)
   WebRequest pageContent = WebRequest.Create(url);
   WebResponse pageResponse = pageContent.GetResponse();
   Stream stream = pageResponse.GetResponseStream();
   StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
   string readcontent = reader.ReadToEnd();
   Match TitleMatch = Regex.Match(readcontent, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
   string pageTitle = TitleMatch.Groups[1].Value;
   return pageTitle;


Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

  • user

    AUTHOR tk

    Posted on 8:57 am August 15, 2018.

    got error on 2nd one.
    An existing connection was forcibly closed by the remote host

  • This site uses Akismet to reduce spam. Learn how your comment data is processed.

    View Comments (1) ...