WebClient.QueryString プロパティ
要求に関連付けられているクエリの名前/値ペアのコレクションを取得または設定します。
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);
プロパティ値
要求に関連付けられているクエリの名前/値ペアを格納する NameValueCollection 。
解説
QueryString プロパティは、クエリ文字列として URI に追加される名前/値ペアを含む NameValueCollection インスタンスを格納します。 QueryString プロパティの内容の前には疑問符 (?) が付き、名前/値ペアはアンパサンド (&) で区切られます。
使用例
[Visual Basic, C#, C++] コマンド ラインからユーザー入力を受け取り、 QueryString プロパティに割り当てる NameValueCollection を構築する例を次に示します。その後、サーバーからローカル ファイルに応答をダウンロードします。
Dim uriString As String = "https://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 = "https://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"https://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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard