Envoyer d'une requête HTTP

//Classe permettant l"envoi d"une requète HTTP en mode POST

 

using System;

using System.Net;

using System.IO;

 

namespace RequeteHTTP

     {

          class Program

          {

               static void Main(string[] args)

                    {

                     String webservice;

                     String valeurJson;

                     webservice          = "https://adresse-placebo.com/api/index.php?key=12345";

                     valeurJson          = "0";

                     WebRequest request  = WebRequest.Create(webservice);

                     request.Method      = "POST";

                     request.ContentType = "application/json";

 

                     using (var streamWriter = new StreamWriter(request.GetRequestStream()))

                         {

                          string json = "{" +

                                        "test":""+valeurJson+ """+

                                        "}";

                                           

                          streamWriter.Write(json);

                          streamWriter.Flush();

                          streamWriter.Close();

                         }

                                    

                     var Reponse = request.GetResponse();

 

                     using (var streamReader = new StreamReader(Reponse.GetResponseStream()))

                     {

                          var Resultat = streamReader.ReadToEnd();

                          Console.WriteLine(Resultat);

                     }

 

                     Console.ReadLine();

           }

     }

}