C # Back-end Post request method

Source: Internet
Author: User

C # Analog Post request method

Method 1:

<summary>///analog POST request///</summary>//<param name= "url" > Address </param>        <param name= "Postdatastr" > Parameters:a=123&b=www</param>//<returns></returns> public string httppost (string url, string postdatastr) {Encoding encode = System.Text.Encoding.GetEnc            Oding ("Utf-8"); byte[] Arrb = encode.            GetBytes (POSTDATASTR);            HttpWebRequest Myreq = (HttpWebRequest) webrequest.create (URL);            Myreq.method = "POST";            Myreq.contenttype = "application/x-www-form-urlencoded";            Myreq.contentlength = Arrb.length;            Stream OutStream = Myreq.getrequeststream ();            Outstream.write (ARRB, 0, arrb.length);            Outstream.close ();            Response received by HTTP WebResponse myresp = Myreq.getresponse ();            Stream Receivestream = Myresp.getresponsestream (); StreamReader Readstream = new StreamReader (receivestreAm, encode);            char[] Read = new char[256];            int count = Readstream.read (Read, 0, 256);            string str = NULL;                while (Count > 0) {str + = new String (read, 0, Count);            Count = Readstream.read (Read, 0, 256);            } readstream.close ();            Myresp.close ();        return str; }

Method 2:

<summary>///Send a POST request///</summary>//<param name= "url" ></param>//<param Name= "POSTDATASTR" ></param>///<returns></returns> public static string HttpPost (string URL,        String postdatastr) {HttpWebRequest request = (HttpWebRequest) webrequest.create (URL); Request.        Method = "POST"; Request.        ContentType = "application/x-www-form-urlencoded"; Request.        ContentLength = Encoding.UTF8.GetByteCount (POSTDATASTR); Stream Myrequeststream = Request.        GetRequestStream ();        StreamWriter mystreamwriter = new StreamWriter (Myrequeststream, encoding.getencoding ("gb2312"));        Mystreamwriter.write (POSTDATASTR);        Mystreamwriter.close (); HttpWebResponse response = (HttpWebResponse) request.        GetResponse (); Stream Myresponsestream = Response.        GetResponseStream ();        StreamReader Mystreamreader = new StreamReader (Myresponsestream, encoding.getencoding ("Utf-8")); String retstring = Mystreamreader.readtoend ();        Mystreamreader.close ();        Myresponsestream.close ();    return retstring; }

  

C # Back-end 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.