WebClient.QueryString 屬性

定義

取得或設定與要求相關聯的查詢名稱/值組集合。

C#
public System.Collections.Specialized.NameValueCollection QueryString { get; set; }

屬性值

NameValueCollection,其中包含與要求相關聯的查詢名稱/值組。 如果沒有配對與要求相關聯,則值會是空的 NameValueCollection

範例

下列程式代碼範例會從命令行取得使用者輸入,並建置指派給 QueryString 屬性的 NameValueCollection。 然後,它會將伺服器的回應下載到本機檔案。

C#
string uriString = "http://www.contoso.com/search";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Create a new NameValueCollection instance to hold the QueryString parameters and values.
NameValueCollection myQueryStringCollection = new NameValueCollection();
Console.Write("Enter the word(s), separated by space character to search for in " +  uriString + ": ");
// Read user input phrase to search for at uriString.
string searchPhrase = Console.ReadLine();
if (searchPhrase.Length > 1)
    // Assign the user-defined search phrase.
    myQueryStringCollection.Add("q",searchPhrase);
else
    // If error, default to search for 'Microsoft'.
    myQueryStringCollection.Add("q","Microsoft");		
// Assign auxilliary parameters required for the search.
Console.WriteLine("Searching " + uriString + " .......");
// Attach QueryString to the WebClient.
myWebClient.QueryString = myQueryStringCollection;
// Download the search results Web page into 'searchresult.htm' for inspection.
myWebClient.DownloadFile (uriString, "searchresult.htm");
Console.WriteLine("\nDownload of " + uriString + " was successful. Please see 'searchresult.htm' for results.");

備註

警告

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

QueryString 屬性包含 NameValueCollection 實例,其中包含附加至 URI 做為查詢字串的名稱/值組。 QueryString 屬性的內容前面加上問號(?),名稱/值組會以連字元分隔(&)。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1