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);
}
}
}