C# WebClient Credentials

Description

WebClient Credentials Gets or sets the network credentials that are sent to the host and used to authenticate the request.

Syntax

WebClient.Credentials has the following syntax.


public ICredentials Credentials { get; set; }

Example

The following code example uses the user's system credentials to authenticate a request.


using System;/*  w  w w  . j  a  v a2s  .  c  om*/
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Example
{
    public static void Main()
    {
        WebClient client = new WebClient();

        client.Credentials = CredentialCache.DefaultCredentials;

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

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Net »




Cookie
Dns
IPAddress
IPEndPoint
IPHostEntry
WebClient