c# internet request code example

Example 1: how make a post request c#

//You can aquire RestSharp from Nuget package manager
  RestClient restClient = 
  new RestClient(string.Format("{0}/myservice/api/endpoint", "https://exampledomain.com:88"));
  RestRequest request = new RestRequest(Method.POST);
  request.RequestFormat = DataFormat.Json;
  request.AddJsonBody(data); //data is C# model for post request
  request.AddHeader("Accept", "application/pdf");
  var result = restClient.Execute(request);

Example 2: .net core c# webrequest download

byte[] listeByte;
            try
            {
                
                FtpWebRequest requestFichier = (FtpWebRequest)WebRequest.Create(@filePath);
                requestFichier.Credentials = new NetworkCredential(utilisateur, motDePasse);
                requestFichier.Method = WebRequestMethods.Ftp.DownloadFile;
                using (WebResponse response = await requestFichier.GetResponseAsync())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        response.GetResponseStream().CopyTo(ms);
                        listeByte = ms.ToArray();
                    }
                }
            }
            catch (Exception ex)
            {
               throw ex;
            }

            return listeByte;