C # HTTP GET vs. Post Request method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.