C # request http post and get,
First of all, I would like to thank the blog provided by the blogger Xiaowei for solving the problem.
I also asked the question first. What do we need to request http?
When I request http to pass in my parameters, I want to get some data in the project. For example, here we need to get SceneList.
1. input the catagoryid to obtain the scenario list under this category.
2. Print the scenario list information on the console.
First, GET Method
Static void Main (string [] args) {// GET method // first create an httpRequest and use Webrequest. create (url) method HttpWebRequest httpRequest = (HttpWebRequest) WebRequest. create ("http: // localhost: 5534/Home/SceneList? Catagoryid = 2 "); // initialize httpRequest and declare that the GET method is used to request http httpRequest. timeout = 2000; httpRequest. method = "GET"; // create an httpResponse to store the Server Response Information HttpWebResponse httpResponse = (HttpWebResponse) httpRequest. getResponse (); // I don't know what to do here. It's all about copying other people's documents. // read the page. StreamReader sr = new StreamReader (httpResponse. getResponseStream (), System. text. encoding. UTF8); // After the page is read, the result is received as string result = sr. readToEnd (); // a new string. Replace all specified strings in the current instance with another specified string Replace (string oldValue, string newValue) result = result. replace ("\ r ",""). replace ("\ n ",""). replace ("\ t", ""); // request status feedback int status = (int) httpResponse. statusCode; sr. close (); Console. writeLine (result); Console. readKey ();}GET
Then the POST method
Static void Main (string [] args) {// POST method // instantiate an encoding method UTF8Encoding encoding = new UTF8Encoding (); // input the parameter string postData = "catagoryid = 2"; // encode the parameter in the UTF-8 format byte [] data = encoding. getBytes (postData); HttpWebRequest myRequest = (HttpWebRequest) WebRequest. create ("http: // localhost: 5534/Home/SceneList"); myRequest. method = "POST"; myRequest. contentType = "application/x-www-form-urlencoded"; myRequest. contentLength = data. length; Stream newStream = myRequest. getRequestStream (); newStream. write (data, 0, data. length); newStream. close (); HttpWebResponse myResponse = (HttpWebResponse) myRequest. getResponse (); StreamReader reader = new StreamReader (myResponse. getResponseStream (), Encoding. UTF8); string result = reader. readToEnd (); result = result. replace ("\ r ",""). replace ("\ n ",""). replace ("\ t", ""); int status = (int) myResponse. statusCode; reader. close (); Console. writeLine (result); Console. readKey ();}POST
In fact, POST and GET are similar, that is, the method for passing parameters is different. The GET method is to input the parameter http: // localhost: 5534/Home/SceneList after the url? Catagoryid = 2
The POST method first declares the parameter, then transcodes it, and then carries the newStream. Write (data, 0, data. Length) parameter when writing );
This solves the current problem. Thanks again to the blog provided by Xiaowei, the blogger.
Note: This article is for reference only. It also has many flaws. The most important thing is not code, but logic.