C # Analog HTTP sends a post or GET request

Source: Internet
Author: User

123456789101112131415161718192021222324252627282930313233343536373839 privatestringHttpPost(stringUrl, stringpostDataStr)        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);            request.Method = "POST";            request.ContentType = "application/x-www-form-urlencoded";            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);            request.CookieContainer = cookie;            Stream myRequestStream = request.GetRequestStream();            StreamWriter myStreamWriter = newStreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));            myStreamWriter.Write(postDataStr);            myStreamWriter.Close();            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            response.Cookies = cookie.GetCookies(response.ResponseUri);            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = newStreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            stringretString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            returnretString;        }        publicstringHttpGet(string Url, stringpostDataStr)        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == """""?") + postDataStr);            request.Method = "GET";            request.ContentType = "text/html;charset=UTF-8";            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = newStreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            stringretString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            returnretString;        }

  

Sometimes also used in the post of the cookie, like login 163 email when you need to send a cookie, so in the external a cookie property at any time to save Cookiecontainer cookie = new Cookiecontainer ();

! Note: Sometimes the request is redirected, but we need to get something from the redirect URL, such as the success of the QQ login to get the SID, but the above will automatically follow the redirect address to jump. We can use:
Request. AllowAutoRedirect = false; Set redirect disabled, you can get the redirect address from the Headers Location property

C # impersonate HTTP to send a post or GET request

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.