Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I need to block a website from my vb.net web application. Requirement is when am running my application I need to block request/response to a specific website through my application.

I've tried in so many ways, but the codes given below doesn't gives me the exact solution.

1)

VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        path = "C:\Windows\System32\drivers\etc\hosts"
        sw = New StreamWriter(path, True)
        Dim sitetoblock As String = "\n 127.0.0.1 xyz.com"
        sw.Write(sitetoblock)
        sw.Close()
        Response.Write("<script>alert('Site Blocked'); </script>")
    End Sub


2)
JavaScript
<script type="text/javascript" language="javascript">
     function BlockUrl() 
      {

            var path = "C:\Windows\System32\drivers\etc\hosts";
            var sw = new StreamWriter(path, True);
            var sitetoblock = vbLf & " 127.0.0.1 xyz.com";
            sw.Write(sitetoblock);
            sw.Close();
      }


Please give some solutions for this...

Thanks and regards,
Ashok
Posted
Updated 15-Jul-13 3:35am
v2

1 solution

This little technique isn't going to do anything for you. First, it only blocks the name resolution of the target site, not block the transmission of traffic.

Why doesn't it work?? Because the machine already had the DNS name resolved to an IP and cached in the DnsCache. You'd need to clear the cache every time you modify the hosts file.

BUT, this ONLY stops your from resolving the name to an IP address. If the user types in the IP address in place of the DNS name in the URL, your little block won't work at all.

THe only way to block transmission of traffic is to write a proxy server! I hope you know TCP/IP client/server communication because you're going to be swimming in it in this project.
 
Share this answer
 
Comments
Dave Kreskowiak 16-Jul-13 7:29am    
From the OP: But i've also tried in some other ways, *) Setting the proxy disable in web.config under configuration, <proxy/> *) Blocking the specific ip through security in web.config, Above samples also not providing me the exact output. Help me Thanks & Regards, Ashok
Dave Kreskowiak 16-Jul-13 7:30am    
No, I said you have to WRITE A PROXY SERVER, not change a setting for proxies in web.config.

Or, you can just buy and use a proxy server "off-the-shelf". This is standard functionality for any proxy server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900