C# Programmation Réseau: Se connecter un serveur HTTP avec les identifiants

Author:

using System;
using System.IO;
using System.Net;

public class HttpPut {

  public static void Main(string [] args) {

    string url = "http://www.mesexemples.com/";
    string username = "username";
    string password = "password";
    string data = "Les données du serveur.";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "PUT";
    request.Credentials = new NetworkCredential(username, password);
    request.ContentLength = data.Length;
    request.ContentType = "text/plain";

    using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) {
      writer.WriteLine(data);
    }

    WebResponse response = request.GetResponse( );

    using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) {
      while (reader.Peek( ) != -1) {
        Console.WriteLine(reader.ReadLine( ));
      }
    }
  }
}

           
       

Leave a Reply

Your email address will not be published. Required fields are marked *