WebClient 的 BaseAddress 和 QueryString 属性有何用途?

发布于 2024-12-06 18:33:20 字数 712 浏览 0 评论 0原文

我正在尝试实例化一个 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 中的所有方法(DownloadDataDownloadFileDownloadStringOpenRead 等)需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-12-13 18:33:20

如果您想使用这些查询参数访问 http://contoso.com/test.html,您可以编写:

wc.DownloadString("test.html");

换句话说,BaseAddressQueryString当您从同一站点下载多个页面时,最好使用

否则,使用 UriUriBuilder 类构造您自己的绝对 Uri,并将完全形成的 Uri 传递给 DownloadString (或您需要的任何方法)打电话)。

If you wanted then to access http://contoso.com/test.html with those query parameters, you could write:

wc.DownloadString("test.html");

In other words, BaseAddress and QueryString are best used when you're downloading multiple pages from the same site.

Otherwise, construct your own absolute Uri using the Uri or UriBuilder classes, and pass in the fully formed Uri to DownloadString (or whatever method you need to call).

浅听莫相离 2024-12-13 18:33:20

来自 http://msdn.microsoft.com/en- us/library/system.net.webclient.baseaddress.aspx

BaseAddress 属性包含一个基本 URI,它与
相对地址。当你调用上传或下载的方法时
数据,WebClient 对象将此基本 URI 与相对的 URI 结合起来
您在方法调用中指定的地址。如果您指定绝对值
URI,WebClient 不使用 BaseAddress 属性值。

所以 BaseAddress 正在 WebClient 上执行它应该为所有可以调用的方法执行的通用操作。可以在重复使用这个一次性配置的 Web 客户端实例后调用多个方法。

该方法本身负责给出相对于 BaseAddress 的执行路径,或者覆盖预先配置的 BaseAddress 的绝对路径。

对我来说听起来很合乎逻辑:-)

From http://msdn.microsoft.com/en-us/library/system.net.webclient.baseaddress.aspx:

The BaseAddress property contains a base URI that is combined with a
relative address. When you call a method that uploads or downloads
data, the WebClient object combines this base URI with the relative
address you specify in the method call. If you specify an absolute
URI, WebClient does not use the BaseAddress property value.

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文