usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Linq;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Net;usingSystem.IO;usingSystem.Text;usingSystem.Collections.Generic;namespacewebapptest{ Public classHttptools { Public Static stringGetrequest (stringURL) {HttpWebRequest Request=(HttpWebRequest) httpwebrequest.create (URL); Request. Method="Get"; HttpWebResponse Response=(HttpWebResponse) request. GetResponse (); using(StreamReader sr =NewStreamReader (response. GetResponseStream (), Encoding.UTF8)) {stringresult =Sr. ReadToEnd (); returnresult; } } Public Static stringPostrequest (stringurl,dictionary<Object,Object>paramlist) {HttpWebRequest Request=(HttpWebRequest) httpwebrequest.create (URL); StringBuilder SB=NewStringBuilder (); foreach(varIteminchparamlist) {sb. Append (item. Key+"="+ Item. Value +"&"); } //The parameter is spelled as "name=test&pwd=123", and then the string is converted to a byte array and the byte array is written to the request stream stringParamdata = sb. ToString (). Trim ('&'); byte[] data =System.Text.Encoding.UTF8.GetBytes (Paramdata); //Set Post modeRequest. Method ="Post"; //it can't be easy. Post requests do not get the corresponding response resultsRequest. ContentType ="application/x-www-form-urlencoded"; //set the length of the request parameterRequest. ContentLength =data. Length; Stream Stream=request. GetRequestStream (); Stream. Write (data,0, data. Length); Stream. Close (); /** * ***************** Note ******************** * * The response stream that was received at the end of a GET or POST request cannot be directly Strea M * not hard to respond results * * * Direct use of stream cannot get the result value of the response * * To use StreamReader to get the result value of the response * * Stream stream = response. GetResponseStream (); * * byte[] data = new BYTE[2*1024*1024] * * int r = stream. Read (Data,0,data. Length); * * String result = System.Text.Encoding.UTF8.GetString (data, 0, R); * * * **/HttpWebResponse Response=(HttpWebResponse) request. GetResponse (); using(StreamReader sr =NewStreamReader (response. GetResponseStream (), Encoding.UTF8)) {stringresult =Sr. ReadToEnd (); returnresult; } } }}
C # HTTP GET vs. Post Request method