I am trying to instantiate a WebClient as follows:
WebClient wc = new WebClient();
wc.BaseAddress = "http://contoso.com";
wc.QueryString.Add("ctNm", "some name");
wc.QueryString.Add("port", "title");
wc.QueryString.Add("rx", "1");
wc.QueryString.Add("own", "userx");
wc.QueryString.Add("asOfDt", "02/23/2011");
Since I already defined everything I need for my web request (I mean, I have BaseAddress and QueryString defined), I thought I was going to find some sort of method that would allow me to issue the request without passing in any additional parameters. To my surprise, all methods in WebClient (DownloadData, DownloadFile, DownloadString,OpenRead, etc.) require a Uri or a string as parameter.
What’s the point in having a BaseAddress and a QueryString properties that you can add values to if you still have to construct the URL manually in order to issue the request? Am I using the wrong tool here? Should I be using WebRequest instead?
If you wanted then to access
http://contoso.com/test.htmlwith those query parameters, you could write:In other words,
BaseAddressandQueryStringare best used when you’re downloading multiple pages from the same site.Otherwise, construct your own absolute Uri using the
UriorUriBuilderclasses, and pass in the fully formed Uri toDownloadString(or whatever method you need to call).