SoFunction
Updated on 2025-03-07

C# winform request http implementation (get, post)

1: There are two classes in .Net: HttpWebRequest and HttpWebResponse classes to implement Http requests

Implementation steps:

1. Create an HttpWebRequest object through the WebRequest class, which can contain Http request information.
2. Setting the HttpWebRequest object is actually setting the information content of the Http request message.
3. Get the HttpWebResponse object from the HttpWebRequest object, which contains the Http response information.
4. Obtain response header information and response subject information from the response information.

Two: HTTP defines different ways to interact with the server

The basic methods include GET, POST, PUT, DELETE, respectively, for checking, adding, and deleting. Generally, we only use GET and POST.

Differences between POST and GET

It is to obtain data from the server, and POST is to transfer data to the server.
It is to add the parameter data queue to the URL referred to by the ACTION attribute of the submitted form. The value corresponds to each field in the form one by one, and can be seen in the URL. POST is to pass each field in the form and its contents into the HTML HEADER through the HTTPPOST mechanism to transmit it to the URL address referred to by the ACTION attribute. Users cannot see this process.
3. For the GET method, the server side uses the value of the variable, and for the POST method, the server side uses the value of the submitted data.
The amount of data transmitted is small and cannot be greater than 2KB (this is mainly due to the URL length limitation). The amount of data transmitted by POST is large and is generally defaulted to be unrestricted. But theoretically, the limitation depends on the server's processing power.
Low security and high POST security. Because during the transmission process of GET, the data is placed in the requested URL, and now many existing servers, proxy servers or user agents will record the requested URL in the log file and then place it somewhere, so that some privacy information may be seen by third parties. In addition, users can also directly see the submitted data on the browser, and some internal system messages will be displayed in front of the user. All operations of POST are invisible to the user.

Two simple Get requests and Post requests

ask

string strURL = "http://localhost/?tel=11111&name=Zhang San"; request;
// Create an HTTP requestrequest = ()(strURL);
//="get";
 response;
response = ()();
 myreader = new ((), Encoding.UTF8);
string responseText = ();
();
(responseText);

ask

string strURL = "http://localhost/";
 request;
request = ()(strURL);
//Post request method = "POST";
// Content Type = "application/x-www-form-urlencoded";
// The parameters are URL encodedstring paraUrlCoded = ("keyword");
paraUrlCoded += "=" + ("Multiple Months");
byte[] payload;
//Convert URL encoded string into bytespayload = .(paraUrlCoded);
//Set the requested ContentLength = ;
//Get request stream writer = ();
//Write request parameters to stream(payload, 0, );
// Close the request flow();
 response;
// Get the response flowresponse = ()();
 myreader = new ((), Encoding.UTF8);
string responseText = ();
();
(responseText);

A small test

Requesting content on Baidu's homepage ( ) means obtaining the html content on Baidu's homepage.

Create an HttpWebRequest request and set the request message information

 //askstring uri = ;
HttpWebRequest request = (uri) as HttpWebRequest;
 = "GET";                            //Request method = new Version(1, 1);   //Http/1.1 version//Add Other ...

Receive response, output response header information and subject information

HttpWebResponse response=() as HttpWebResponse;
   //Header
   foreach (var item in )   
  {
     this.txt_Header.Text += ()+": " +   
       (())
     + ;
  }
 
    //If the subject information is not empty, the subject information content will be received   if ( <= 0)
      return;
   //Receive response subject information   using(Stream stream =())
   {
     int totalLength=(int);
     int numBytesRead=0;
     byte[] bytes=new byte[totalLength+1024];
    //Read the data in the stream through a loop, and after reading, the loop will be out of the loop     while( numBytesRead < totalLength  )
     {
      int num=(bytes,numBytesRead,1024);  //I hope to read 1024 bytes each time      if( num==0 )   //Instructions that the data in the stream has been read.        break;
      numBytesRead+=num;
     }
 
  }
   //Show the received subject data to the interface   string content=Encoding.(bytes);
   this.txt_Content.Text=content;

This is the end of this article about the implementation (get, post) of C# winform request http. For more related content for C# winform request http, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!