C # simple webpage access sample

Source: Internet
Author: User

Write down the backup.

Synchronous call of httpwebrequest

UriBuilder uri = new UriBuilder("http://www.cnblogs.com/xiaokang088/rss.aspx");            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri);            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            var stream = response.GetResponseStream();            StreamReader reader = new StreamReader(stream);            var str = reader.ReadToEnd();

Httpwebrequest asynchronous call

 public MainWindow()        {            InitializeComponent();            Loaded += new RoutedEventHandler(MainWindow_Loaded);        }        void MainWindow_Loaded(object sender, RoutedEventArgs e)        {            UriBuilder uri = new UriBuilder("http://www.cnblogs.com/xiaokang088/rss.aspx");            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri);            request.BeginGetResponse(new AsyncCallback(handleResponce), request);        }        private void handleResponce(IAsyncResult ar)        {            HttpWebRequest request = ar.AsyncState as HttpWebRequest;            var reponse = request.EndGetResponse(ar) as HttpWebResponse;            var stream = reponse.GetResponseStream();            StreamReader reader = new StreamReader(stream);            string str = reader.ReadToEnd();        }

WebClient, which is the simplest. Pay attention to encoding; otherwise, garbled characters will occur.

  WebClient client = new WebClient();            //client.Encoding = Encoding.UTF8;            var address = "http://www.cnblogs.com/xiaokang088/rss.aspx";            string content = client.DownloadString(address);

 

Finished!

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.