HttpRequest Head and Body

    0

    0

    h4r7w3l1

    The code above first creates an instance of the HttpWebRequest class, using the string site as the input. It then uses the HttpWebRequest.Create() method to create a request to the httpbin.org website. The HttpWebResponse object that is returned from this request contains the body of the response sent from httpbin.org.

    The code uses the StreamReader class to read the entire body of the response, line by line. The textText variable is populated with the contents of each line in the response.

    using System;
    using System.Net;
    using System.IO;
    
    public class Program
    {
      public static void Main()
      {
      string site = "https://httpbin.org/get";
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(site);
      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
      Console.WriteLine(req);
      Console.WriteLine(resp);
      using (StreamReader stream = new StreamReader(resp.GetResponseStream()))
      {
        var txb_textText = stream.ReadToEnd();
        Console.WriteLine(txb_textText);
      }
    
      Console.WriteLine(resp.Headers);
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.