次の方法で共有


WebClient コンストラクタ

WebClient クラスの新しいインスタンスを初期化します。

Public Sub New()
[C#]
public WebClient();
[C++]
public: WebClient();
[JScript]
public function WebClient();

解説

既定のコンストラクタは、各フィールドを null 参照 (Visual Basic では Nothing) に設定して、 WebClient クラスの新しいインスタンスを作成します。

使用例

[Visual Basic, C#, C++] WebClient インスタンスを作成し、次にそれを使用して、サーバーからデータをダウンロードしてシステム コンソールに表示し、サーバーからデータをダウンロードしてファイルに書き込み、サーバーにフォーム値をアップロードして応答を受信する例を次に示します。

 
Public Shared Sub Main()


    Try
        Dim client As New WebClient()
        Dim pageData As [Byte]() = client.DownloadData("https://www.contoso.com")
        Dim pageHtml As String = Encoding.ASCII.GetString(pageData)

        ' Download the data to a buffer.
        Console.WriteLine(pageHtml)
        
        ' Download the data to a file.
        client.DownloadFile("https://www.contoso.com", "page.htm")
        
        
        ' Upload some form post values.
        dim form as New NameValueCollection()
           form.Add("MyName", "MyValue")  

           ' Note that you need to replace "https://localhost/somefile.aspx" with the name of 
           ' a file that is available to your computer.
           Dim responseData As [Byte]() = client.UploadValues("https://www.contoso.com/form.aspx", form)      
        Console.WriteLine(Encoding.ASCII.GetString(responseData))
    
    Catch webEx As WebException
        if webEx.Status = WebExceptionStatus.ConnectFailure then
           Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.")
        end if
        Console.Write(webEx.ToString())
    End Try
    

End Sub 'Main

[C#] 
try {
    
// Download the data to a buffer.
       WebClient client = new WebClient();

  Byte[] pageData = client.DownloadData("https://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);

// Download the data to a file.
        client.DownloadFile("https://www.contoso.com", "page.htm");

// Upload some form post values.
NameValueCollection form = new NameValueCollection();        
form.Add("MyName", "MyValue");        
Byte[] responseData = client.UploadValues("https://www.contoso.com/form.aspx", form);        

}
catch (WebException webEx) {
      Console.WriteLine(webEx.ToString());
       if(webEx.Status == WebExceptionStatus.ConnectFailure) {
           Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
       }
}

[C++] 
try {

    // Download the data to a buffer.
    WebClient* client = new WebClient();

    Byte pageData[] = client->DownloadData(S"https://www.contoso.com");
    String* pageHtml = Encoding::ASCII->GetString(pageData);
    Console::WriteLine(pageHtml);

    // Download the data to a file.
    client->DownloadFile(S"https://www.contoso.com", S"page.htm");

    // Upload some form post values.
    NameValueCollection* form = new NameValueCollection();        
    form->Add(S"MyName", S"MyValue");        
    Byte responseData[] = client->UploadValues(S"https://www.contoso.com/form.aspx", form);        

}
catch (WebException* webEx) {
    Console::WriteLine(webEx->ToString());
    if(webEx->Status == WebExceptionStatus::ConnectFailure) {
        Console::WriteLine(S"Are you behind a firewall?  If so, go through the proxy server.");
    }
}

[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

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間