C #: Return value of a href

0

How do I get the address / URL of a tag

 < a href="js:redir(2)">
Through an HTML source?

NOTE: I tried to use the mshtml assembly, but it did not work either!

    
asked by anonymous 16.07.2014 / 14:58

2 answers

1

You can use this example to deploy to your project , regular expression is used.

C #

private static void DumpHRefs(string inputString)
{
    Match m;
    string HRefPattern = "href\s*=\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\S+))";

    try
    {
         m = Regex.Match(inputString, HRefPattern,
         RegexOptions.IgnoreCase | RegexOptions.Compiled,
         TimeSpan.FromSeconds(1));
         while (m.Success)
         {
             Console.WriteLine(m.Groups[1]);
             m = m.NextMatch();
         }
    }
    catch (RegexMatchTimeoutException)
    {
         Console.WriteLine("The matching operation timed out.");
    }
}

And to use the function just send the code HTML as a parameter.

C #

string inputString = "My favorite web sites include:</P>" +
                        "<A HREF=\"http://msdn2.microsoft.com\">" +
                        "MSDN Home Page</A></P>" +
                        "<A HREF=\"http://www.microsoft.com\">" +
                        "Microsoft Corporation Home Page</A></P>" +
                        "<A HREF=\"http://blogs.msdn.com/bclteam\">" +
                        ".NET Base Class Library blog</A></P>";

DumpHRefs(inputString);

Result

  

link

     

link

     

link

The project was building using console, just adapt the function to your project.

NOTE: You may need to include the reference below

using System.Text.RegularExpressions;
    
16.07.2014 / 22:51
-1

If I understand what you need, you can do it as follows:

<htmL>
<body>
A url completa da página atual é:
<script>
document.write(document.URL);
</script>

</body>
</html>

document.write(window.location.href)

Other ways:

document.write(window.location.protocol) 
document.write(window.location.host) 
document.write(window.location.pathname) 

The code above will display:

"http"
"teste.com.br"
"login/index.html"
    
16.07.2014 / 21:38