Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net.Http;
- namespace WindowsFormsApplication1 {
- public partial class Form1 : Form {
- public Form1() {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e) {
- var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); //запоминаем планировщий ui-потока
- var uri = "http://yandex.ru";
- parseAndUpdateRtb(uri, uiScheduler);
- }
- void parseAndUpdateRtb(string uri, TaskScheduler scheduler) {
- var client = getHttpClient();
- var task = client
- .GetStringAsync(uri) //получаем страницу
- .ContinueWith(t => parseRespose(t.Result)) // парсим результат
- .ContinueWith(t => richTextBox1.Text += t.Result, scheduler); //обновляем содержие окна
- Task.Run(() => task); //запускем на выполнение цепочку задач
- }
- string parseRespose(string response) {
- return response + Environment.NewLine;
- }
- HttpClient getHttpClient() {
- var http = new HttpClient();
- http.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*");
- http.DefaultRequestHeaders.Add("Accept-Language", "ru-RU");
- http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
- return http;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement