SoFunction
Updated on 2025-03-07

Introduction to four ways of calling interfaces in C#

When calling an interface with C#, you need to call the login interface before calling other interfaces. Because other interfaces need to save the cookie value under login state to have permission to call, so first you need to save the cookie value by calling the login interface, and then call other interfaces

1. Through Get

        #region get method
        public string HttpGet(string url)
        {

            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)(url);
             = "GET";
             = "application/json";
            ["Accept-Encoding"] = "gzip,deflase";
             = ;
            HttpWebResponse response = (HttpWebResponse)();

            // HttpCookie cookies = new HttpCookie("admin"); //If you need to save the cookie value through login, you can add a part of it            // = (["Set-Cookie"]); // Read http data with cookies in response to request            //  = (1);
            //  (cookies);

            using (StreamReader reader = new StreamReader((), Encoding.UTF8))
            {
                return ();
            }
        }
        #endregion

However, not all get requests need to add this hear content. Some add this may cause garbled code, so do not set the Accept-Encoding header

The reason for adding this header here is that when you see the browser obtained in the web page analysis tool browsing the web page, this is how the corresponding http header content is set.

Therefore, in the code, it also simulates the browser to access the web page, and sets the corresponding Accept-Encoding to gzip, deflate

The reason why ordinary browsers access web pages is added: "Accept-Encoding" = "gzip,deflate"

That's because the browser will automatically decompress the corresponding gzip compressed web page returned from the server. Therefore, when requesting, add the corresponding header to indicate that it accepts the compressed data.

At the same time, the sentence = ; can be added to obtain the correct data.

If you get the web page content too large, you can still use this method, so that HttpWebRequest can automatically help you decompress, which can reduce the amount of data transmission, save time and improve efficiency.

2. By post

public string HttpPost2(string url, string body)
{

   //Convert the data sent by the user into a byte stream of "UTF-8"    Encoding encoding = Encoding.UTF8;
    //First construct the request address based on the uri requested by the user    //Create a Web access object    HttpWebRequest request = (HttpWebRequest) (url);
     = "POST";
    //  = "application/json";
    = "application/json; charset=UTF-8";
   ["Accept-Encoding"] = "gzip, deflate";
    = ;
   //HttpCookie Cookie = ["admin"]; //If you need to log in before accessing the data of the obtaining url, you need to set the cookie value in the request header   //if (Cookie != null)
   //    ("Cookie", ());

   byte[] buffer = (body);
    = ;
   ().Write(buffer, 0, );
   //Get response content through web access object   HttpWebResponse response = (HttpWebResponse) ();
   //Create StreamReader object by responding to content streams, because StreamReader is more advanced and faster   using (StreamReader reader = new StreamReader((), Encoding.UTF8))
   {
    return ();//Use StreamReader to read from the beginning to the end of the response content   }
 }

3. Request via put

        #region Put request        public string Put(string data, string uri)
        {//Create a Web access object            HttpWebRequest Request = (HttpWebRequest)(uri);
            //Convert the data sent by the user into a byte stream of "UTF-8"            byte[] buf = ("UTF-8").GetBytes(data);

             = "PUT";
             = ;
             = "application/json";
             = 1;
             = true;
            //Send a request            Stream stream = ();
            (buf, 0, );
            ();

            //Get the interface return value            //Get response content through web access object            HttpWebResponse Response = (HttpWebResponse)();
            //Create StreamReader object by responding to content streams, because StreamReader is more advanced and faster            StreamReader reader = new StreamReader((), Encoding.UTF8);
            //string returnXml = (());//If there is any encoding problem, use this method            string returnXml = ();//Use StreamReader to read from the beginning to the end of the response content            ();
            ();
            return returnXml;

        }           
        #endregion

4. Request via Delete

        #region Delete Request        public string Delete(string data, string uri)
        {
            //Create a Web access object            HttpWebRequest Request = (HttpWebRequest)(uri);
            //Convert the data sent by the user into a byte stream of "UTF-8"            byte[] buf = ("UTF-8").GetBytes(data);

             = "DELETE";
             = ;
             = "application/json";
             = 1;
             = true;
            //Send a request            Stream stream = ();
            (buf, 0, );
            ();

            //Get the interface return value            //Get response content through web access object            HttpWebResponse Response = (HttpWebResponse)();
            //Create StreamReader object by responding to content streams, because StreamReader is more advanced and faster            StreamReader reader = new StreamReader((), Encoding.UTF8);
            //string returnXml = (());//If there is any encoding problem, use this method            string returnXml = ();//Use StreamReader to read from the beginning to the end of the response content            ();
            ();
            return returnXml;

        }
       #endregion

Different scenario requirements, different methods are used to apply in different scenarios.

Through these combinations, the http interface can be called to complete the call and test.

This is all about this article about C# calling interface. I hope it will be helpful to everyone's learning and I hope everyone will support me more.