WebClient 的 BaseAddress 和 QueryString 属性有何用途?
我正在尝试实例化一个 WebClient
,如下所示:
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");
由于我已经定义了网络请求所需的所有内容(我的意思是,我定义了 BaseAddress 和 QueryString),所以我想我会找到某种方法允许我发出请求而无需传递任何其他参数。令我惊讶的是,WebClient
中的所有方法(DownloadData
、DownloadFile
、DownloadString
、OpenRead
等)需要 Uri 或字符串作为参数。
如果您仍然需要手动构造 URL 才能发出请求,那么拥有可以添加值的 BaseAddress 和 QueryString 属性有什么意义呢?我在这里使用了错误的工具吗?我应该改用 WebRequest
吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用这些查询参数访问
http://contoso.com/test.html
,您可以编写:换句话说,
BaseAddress
和QueryString当您从同一站点下载多个页面时,最好使用
。否则,使用
Uri
或UriBuilder
类构造您自己的绝对 Uri,并将完全形成的 Uri 传递给DownloadString
(或您需要的任何方法)打电话)。If you wanted then to access
http://contoso.com/test.html
with those query parameters, you could write:In other words,
BaseAddress
andQueryString
are best used when you're downloading multiple pages from the same site.Otherwise, construct your own absolute Uri using the
Uri
orUriBuilder
classes, and pass in the fully formed Uri toDownloadString
(or whatever method you need to call).来自 http://msdn.microsoft.com/en- us/library/system.net.webclient.baseaddress.aspx:
所以 BaseAddress 正在 WebClient 上执行它应该为所有可以调用的方法执行的通用操作。可以在重复使用这个一次性配置的 Web 客户端实例后调用多个方法。
该方法本身负责给出相对于 BaseAddress 的执行路径,或者覆盖预先配置的 BaseAddress 的绝对路径。
对我来说听起来很合乎逻辑:-)
From http://msdn.microsoft.com/en-us/library/system.net.webclient.baseaddress.aspx:
So BaseAddress is doing the generic thing on the WebClient it is supposed to do for all methods that can be called. Multiple methods can be called after each other re-using this single one time configured web client instance.
The method itself is responsible for giving the path to its execution relative to the BaseAddress, or an absolute path overriding the pre-configured BaseAddress.
Sounds logical to me :-)