C # impersonate an HTTP request

Source: Internet
Author: User

WinForm Sending HTTP requests

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingsystem.web;namespacedingtest.utility.tools{ Public classHttphelper {/// <summary>        ///GET request Mode/// </summary>        /// <param name= "urlstring" >the requested URL</param>        /// <returns></returns>         Public Static stringGet (stringurlstring) {            //Defining local VariablesHttpWebRequest HttpWebRequest =NULL; HttpWebResponse Httpwebrespones=NULL; Stream Stream=NULL; stringHtmlstring =string.            Empty; //Request Page            Try{HttpWebRequest= WebRequest.Create (urlstring) asHttpWebRequest; }            //Handling Exceptions            Catch(Exception ex) {Throw NewException ("An error occurred while creating the page request! ", ex); } httpwebrequest.useragent="mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;. NET CLR 2.0.50727; Maxthon 2.0)"; //get the return information of the server            Try{httpwebrespones=(HttpWebResponse) httpwebrequest.getresponse (); Stream=Httpwebrespones.getresponsestream (); }            //Handling Exceptions            Catch(Exception ex) {Throw NewException ("An error occurred while accepting the server's return page! ", ex); } StreamReader StreamReader=NewStreamReader (stream, Encoding.UTF8); //Read back Page            Try{htmlstring=Streamreader.readtoend (); }            //Handling Exceptions            Catch(Exception ex) {Throw NewException ("An error occurred while reading the page data! ", ex); }            //Releasing resources returns resultsStreamreader.close (); Stream.            Close (); returnhtmlstring; }        /// <summary>        ///POST Request Mode/// </summary>        /// <param name= "url" >the requested URL</param>        /// <param name= "param" >The requested parameter JSON</param>        /// <returns></returns>         Public Static stringPost (stringUrlstringparam) {            stringstrURL =URL;            System.Net.HttpWebRequest request; Request=(System.Net.HttpWebRequest) webrequest.create (strURL); Request. Method="POST"; Request. ContentType="Application/json;charset=utf-8"; stringparaurlcoded =param; byte[] payload; Payload=System.Text.Encoding.UTF8.GetBytes (paraurlcoded); Request. ContentLength=payload.            Length; Stream writer=request.            GetRequestStream (); Writer. Write (Payload,0, payload.            Length); Writer.            Close ();            System.Net.HttpWebResponse response; Response=(System.Net.HttpWebResponse) request.            GetResponse ();            System.IO.Stream s; S=Response.            GetResponseStream (); stringStrdate =""; stringstrvalue =""; StreamReader Reader=NewStreamReader (S, Encoding.UTF8);  while((strdate = Reader.readline ())! =NULL) {strvalue+ = Strdate +"\ r \ n"; }            returnstrvalue; }    }}

C # impersonate an HTTP 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.