Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

WebClient.QueryString Property

Gets or sets a collection of query name/value pairs associated with the request.

[Visual Basic]
Public Property QueryString As NameValueCollection
[C#]
public NameValueCollection QueryString {get; set;}
[C++]
public: __property NameValueCollection* get_QueryString();
public: __property void set_QueryString(NameValueCollection*);
[JScript]
public function get QueryString() : NameValueCollection;
public function set QueryString(NameValueCollection);

Property Value

A NameValueCollection that contains query name/value pairs associated with the request.

Remarks

The QueryString property contains a NameValueCollection instance containing name/value pairs that are appended to the URI as a query string. The contents of the QueryString property are preceded by a question mark (?), and name/value pairs are separated from one another by an ampersand (&).

Example

[Visual Basic, C#, C++] The following example takes user input from the command line and builds a NameValueCollection that is assigned to the QueryString property. It then downloads the response from the server to a local file.

[Visual Basic] 
Dim uriString As String = "http://www.contoso.com/search"
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Create a new NameValueCollection instance to hold the QueryString parameters and values.
Dim myQueryStringCollection As New NameValueCollection()
Console.Write(("Enter the word(s), separated by space characters, to search for in " + uriString + ": "))
' Read user input phrase to search in uriString.
Dim searchPhrase As String = Console.ReadLine()
' Append necessary parameter/value pairs to the name/value container. 
' as QueryString = "?q=Microsoft&btnG=Google+Search". 
If searchPhrase.Length > 1 Then
    'Assign the user-defined search phrase.
    myQueryStringCollection.Add("q", searchPhrase)
' If error, default to search 'Microsoft'.
Else
    myQueryStringCollection.Add("q", "Microsoft")
End If 
' Assign auxilliary parameters required for the search.
myQueryStringCollection.Add("btnG", "Google" + ChrW(43) + "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((ControlChars.Cr + "Download of " + uriString + " was successful. Please see 'searchresult.htm' for results."))

[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.");

[C++] 
String* uriString = S"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(S"Enter the word(s), separated by space character to search for in {0}: ", 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(S"q", searchPhrase);
else
   // If error, default to search for 'Microsoft'.
   myQueryStringCollection->Add(S"q", S"Microsoft");
// Assign auxilliary parameters required for the search.
Console::WriteLine(S"Searching {0} .......", uriString);
// Attach QueryString to the WebClient.
myWebClient->QueryString = myQueryStringCollection;
// Download the search results Web page into 'searchresult.htm' for inspection.
myWebClient->DownloadFile (uriString, S"searchresult.htm");
Console::WriteLine(S"\nDownload of {0} was successful. Please see 'searchresult.htm' for results.", uriString);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace

Show:
© 2015 Microsoft