none
Blocking a specific file in the WebBrowser component? RRS feed

  • Question

  • Hello, MSDN.

    I'm looking for a solution to this - I want to block a specific file (file.swf / file.js, e.g.), but the WebBrowser.Navigating event does not support files like this, how can i do it?

    Sincerely,
    Mikkel Kroman.
    Wednesday, June 3, 2009 5:25 AM

Answers

  • Try the BeforeNavigate event:

    http://support.microsoft.com/kb/325079
    Looking for "entry level" position...
    • Marked as answer by Bin-ze Zhao Monday, June 8, 2009 2:18 AM
    Wednesday, June 3, 2009 9:19 PM
  • Hi,

    For a particular file you can use textbox let user enter the url, use sting.endswith to check if it's valid or not.

    e.g.

           private void button2_Click(object sender, EventArgs e)
            {
                string uu = textBox1.Text;
                if (!uu.EndsWith(".js"))
                {
                    webBrowser1.Navigate(new Uri(textBox1.Text));
                }
                else
                {
                   
                    MessageBox.Show("invalid entry!");
                }
            }
    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, June 8, 2009 2:18 AM
    Thursday, June 4, 2009 9:44 AM

All replies

  • You can handle the Navigating event and if its a blocked file, then cancel the request

    System.Net.WebRequest request = System.Net.WebRequest.Create(e.Url);

    // we need only header part of http response
    request.Method = "HEAD";

    System.Net.WebResponse response = request.GetResponse();

    // only text/html, text/xml, text/plain are allowed... extend as required
    if (!response.ContentType.StartsWith("text/"))
    {
      e.Cancel = true;
      MessageBox.Show("Not allowed for security resons...");
    }



    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Edited by A.m.a.L Hashim Wednesday, June 3, 2009 5:37 AM formatting fixed
    Wednesday, June 3, 2009 5:36 AM
  • Thank you for your answer, but that was nothing like what i intended.

    I don't want to create multiple requests for each navigation to check the content-type header, that would be stupid..
    What i want to do is block/cancel a request to a file (knowing the URL of the file)

    Something like this:
            private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
            {
                if (e.Url.ToString() == "http://anonymus.dk/file.js")
                    e.Cancel = true;
            }
    But i can't, because the Navigating event is not called when the file is getting requested in the WebBrowser component..

    Sincerely,
    Mikkel Kroman.
    Wednesday, June 3, 2009 5:48 AM
  • Not even a single solution?

    Sincerely,
    Mikkel Kroman.
    Wednesday, June 3, 2009 8:58 PM
  • Try the BeforeNavigate event:

    http://support.microsoft.com/kb/325079
    Looking for "entry level" position...
    • Marked as answer by Bin-ze Zhao Monday, June 8, 2009 2:18 AM
    Wednesday, June 3, 2009 9:19 PM
  • Hi,

    For a particular file you can use textbox let user enter the url, use sting.endswith to check if it's valid or not.

    e.g.

           private void button2_Click(object sender, EventArgs e)
            {
                string uu = textBox1.Text;
                if (!uu.EndsWith(".js"))
                {
                    webBrowser1.Navigate(new Uri(textBox1.Text));
                }
                else
                {
                   
                    MessageBox.Show("invalid entry!");
                }
            }
    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, June 8, 2009 2:18 AM
    Thursday, June 4, 2009 9:44 AM