2

before i reference non .net builtin(alternatives)

i would like to know about

The built-in .Net Classes could offer to use :

WebBrowser & WebBrowserBase Classes

what i would like to know is : what are the differences between those two ? .

as msdn noted in Later one - WebBrowserBase -

"This API supports the .NET Framework infrastructure and is not intended to be used directly from your code"

the question began with a simple task : retrieve a file(datasource xml) from a website . sound simple? so you could use any method..., preferably simplest & resource efficient one.

but !

scenario is : that same source (single one to be reliable ) was blocking automatic traffic recently, by using cookies , as i could undersrand WebClient is using the same resources of your main browsers(IE9 in my case) .

....and after a research i have made, using a WebBrowser "Engine" as a file-retriever...

will do the job perfectly .

you could use any (not just Microsoft IE )

about other tests i have made you could visit :

http://seleniumhq.org/docs/03_webdriver.html

+

https://code.google.com/p/selenium/downloads/list

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • 1
    Please rewrite the whole question into two or three lines, because this is unreadable and contains a lot of irrelevant text and newlines. What exactly is wrong with WebClient / WebRequest (as they _do_ support cookies), what is wrong with the WebBrowser (why do you want to use the base class, or whatever?)? – CodeCaster Nov 24 '12 at 17:45
  • @CodeCaster less than this ? question is how to use `WebBrowserBase`, and what can u benefit from it opposed to the usage of sub type : `WebBrowser` – LoneXcoder Nov 24 '12 at 18:03

1 Answers1

6

Your problem

scenario is, that same source (single one to be reliable) was blocking automatic traffic recently by using cookies`

Well, if that is your problem (cookies) Why dont you try this web-client?

public class CookieWebClient : WebClient
{
    private readonly CookieContainer _cookies = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = _cookies;
        }
        return request;
    }
}

WebBrowser vs WebBrowserBase

As for the difference between WebBrowser and WebBrowserBase, WebBrowser inherits WebBrowserBase to extend it and make it into a full functional web-browser. Where WebBrowserBase comes in handy is if you would like to customize the WebBrowser beyond what is supported - you can override things and use WebBrowserBase to extend functionality and such. As a rule, unless you absolutely must, you should stick to just using the plain WebBrowser control - and only if you are rendering web pages or would like to use it to hackishly execute javascript.

Comment about extending WebBrowser in general

One of the most useful modifications I've found personally for the webbrowser was exposing the download flags, so you can control whether the webbrowser downloads images etc. A great example of how to do this is here: https://stackoverflow.com/a/7738174/184746

Community
  • 1
  • 1
caesay
  • 16,932
  • 15
  • 95
  • 160
  • thanks!! do i need to just use:`CookieWebClient cwc = new CookieWebClient();` then `cwc.DownloadFile("www...currency.xml", "G:\Temp.xml")`. is that all? and your **protected** override - `GetWebRequest` not to be touched?( cause it's already doing it's magic on the `webclient` ?) so if i use class as above, the site will check for a cookie that it will not find? (cause when i use **standart** webClient it will expose my Cookies thus source site will hide xml Node via special javascript-cookie trick. is code above in this comment is how i should use your class ? or could you..give an example – LoneXcoder Nov 24 '12 at 18:56
  • can you please try to give an example on how would you customize anything in `WebBrowserBase` (choose any base property or better somthing that does not exist , that i could add as an example to how to a custom behavior that is not supported in derived(and existing) `WebBrowser` or you might just use the `WebBrowser` itself and overRide some of its properties as in your example with WebClient and still not have to tuch its prototype `WebBrowserBase`) otherwise why selinium will come up with their own WebBrowser i want to do somthing similar (not a whole product but just a simple custom addon) – LoneXcoder Nov 24 '12 at 19:33
  • If the `CookieWebClient` class does not work, you will need to give more information as to what is happening, what the website is doing, and why it is not working. – caesay Nov 24 '12 at 20:29
  • it was first commented as i was trying to use it with `DownloadFile` though i really dont nead it to be saved, rather it needed to be extracted and use a value (one of the chilednodes) , so your code is fine !! thanks for that , still i was trying to learn from you about the usage of overriding a class (thus achive custom behavior as you wish) though your example(works!) with the webCient is comfusing and dificult to understand , could you give an example of how would you add any fuctionality to a WebBrowser never mind if it's not by messing with `webBrowserBase` but its child. – LoneXcoder Nov 24 '12 at 20:46