Make Interface implement all http request

0 thích 0 không thích
1 lượt xem
đã hỏi 28 Tháng 2, 2023 trong Lập trình C# bởi nguyenthao (9,000 điểm)
namespace LearnCallBackInterface
{
    public partial class Form1 : Form, IHttpRequestCallback
    {
        MyHttpRequest myHttpRequest;
        public Form1()
        {
            InitializeComponent();
            myHttpRequest = new MyHttpRequest(this);
        }

        public void OnDataReceived(string responsedData)
        {
            richTextBox1.Text = responsedData;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myHttpRequest.MakeHttpRequest("https://laptrinhvb.net");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myHttpRequest.MakeHttpRequest("https://howkteam.vn");
        }
    }

    public interface IHttpRequestCallback
    {
        void OnDataReceived(string responsedData);
    }

    public class MyHttpRequest
    {
        private IHttpRequestCallback _callback;

        public MyHttpRequest(IHttpRequestCallback callback)
        {
            _callback = callback;
        }

        public void MakeHttpRequest(string url)
        {           
            string responsedData = new WebClient().DownloadString(url);             
            _callback.OnDataReceived(responsedData);
            
        }
    }
}

Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...