简体   繁体   English

什么是WebClient的BaseAddress和QueryString属性用于?

[英]What are WebClient's BaseAddress and QueryString properties used for?

I am trying to instantiate a WebClient as follows: 我试图实例化一个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");

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. 由于我已经定义了我的Web请求所需的一切(我的意思是,我已经定义了BaseAddress和QueryString),我想我会找到某种方法来允许我发出请求而不传递任何其他参数。 To my surprise, all methods in WebClient ( DownloadData , DownloadFile , DownloadString , OpenRead , etc.) require a Uri or a string as parameter. 令我惊讶的是, WebClientDownloadDataDownloadFileDownloadStringOpenRead等)中的所有方法都需要Uri或字符串作为参数。

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? 如果您仍然必须手动构造URL以发出请求,那么具有可以添加值的BaseAddress和QueryString属性的重点是什么? Am I using the wrong tool here? 我在这里使用了错误的工具吗? Should I be using WebRequest instead? 我应该使用WebRequest吗?

If you wanted then to access http://contoso.com/test.html with those query parameters, you could write: 如果您希望使用这些查询参数访问http://contoso.com/test.html ,您可以编写:

wc.DownloadString("test.html");

In other words, BaseAddress and QueryString are best used when you're downloading multiple pages from the same site. 换句话说,当您从同一站点下载多个页面时,最好使用BaseAddressQueryString

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). 否则,使用UriUriBuilder类构造自己的绝对Uri,并将完全形成的Uri传递给DownloadString (或者您需要调用的任何方法)。

From http://msdn.microsoft.com/en-us/library/system.net.webclient.baseaddress.aspx : 来自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. BaseAddress属性包含与相对地址组合的基URI。 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. 当您调用上载或下载数据的方法时,WebClient对象将此基URI与您在方法调用中指定的相对地址组合在一起。 If you specify an absolute URI, WebClient does not use the BaseAddress property value. 如果指定绝对URI,则WebClient不使用BaseAddress属性值。

So BaseAddress is doing the generic thing on the WebClient it is supposed to do for all methods that can be called. 因此,BaseAddress正在WebClient上执行它应该为所有可以调用的方法执行的泛型操作。 Multiple methods can be called after each other re-using this single one time configured web client instance. 可以在彼此重新使用此单个一次性配置的Web客户端实例之后调用多个方法。

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. 方法本身负责提供相对于BaseAddress的执行路径,或覆盖预配置BaseAddress的绝对路径。

Sounds logical to me :-) 对我来说很合乎逻辑:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM