This article shared with you C# request http to send data to web pages and receive web pages for your reference. The specific content is as follows
First of all, what do we need?
Request http in POST to transmit data to the web page. After the web page receives the data, it stores the data in the database.
1. First request http, establish a connection, and transmit the transcoded data over
2. The web page receives data and stores it in the database after transcoding.
3. The web page returns something to the transmitter, indicating that we have received the data.
Similarly, we requested http to simulate it using the console.
static void Main(string[] args) { string result = Post("http://localhost:5534/Home/ToUrl", "Family"); (result); (); } /// <summary> /// Specify the Post address to get all strings using Get /// </summary> /// <param name="url">Request background address</param> /// <param name="content">Post Submit Data Content (UTF-8 Encoded)</param> /// <returns>Result</returns> public static string Post(string url, string content) { //Declare a container result to receive data string result = ""; //First create an HttpWebRequest and declare the transmission method POST HttpWebRequest req = (HttpWebRequest)(url); = "POST"; = "application/x-www-form-urlencoded"; //Add POST parameters byte[] data = Encoding.(content); = ; using (Stream reqStream = ()) { (data, 0, ); (); } //Declare a container resp to receive the return data HttpWebResponse resp = (HttpWebResponse)(); Stream stream = (); //Get response content using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = (); } return result; }
Then, there is a ToUrl in the Controller for receiving data
public ActionResult ToUrl() { string result = ""; string jsonStr = "", line; try { Stream streamResponse = ; StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8); while ((line = ()) != null) { jsonStr += line; } (); (); result = jsonStr; } catch (Exception ex) { result = "msg-Data Publish (In) exception:" + ; } Service service = new Service(); //Call AddCatagorys method to add data into (result); //Call the GetCatas method and get the classification list List<Catagory> list = (); //Find the last category in the category list, that is, the category you just added Catagory catagory = list[ - 1]; //Return to Json //return Json(catagory) //Return an ID, Content() has a string type, so int should be converted to a string type return Content(()); }
This is actually equivalent to two people making a phone call. When you call me, you will not call others again.
So here return Content(()); means that the returned ID is returned to the console, that is, the transmitter, so that the transmitter knows that we have received the data you transmitted and save it to the database.
Note: This essay is for reference only, and there are many minor flaws. The most important thing is not the code, logic is the most important.