如何使用 C# 从连续响应的网页中获取数据(部分)?
How to fetch data (partial) from continuously responding webpage with c#?
我们有这个URL
连接不会关闭并继续每隔几秒左右无限期地发送响应。
我想获取部分数据,但使用我当前的代码,它会在显示数据之前等待连接结束或关闭,但由于该网页会无限期响应,我编写的程序也会无限期地等待。
但是对于其他测试 URL,例如 www.google.com 立即关闭连接,它工作正常。
这是我的代码:
public partial class Form2 : Form
{
string testurl = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json";
WebClient wc = new WebClient();
public Form2()
{
InitializeComponent();
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadComplete);
}
private void button1_Click(object sender, EventArgs e)
{
wc.DownloadDataAsync(new Uri(testurl));
button1.Enabled = false;
}
private void DownloadComplete(object sender, DownloadDataCompletedEventArgs args)
{
byte[] result = args.Result;
richTextBox1.Text += System.Text.Encoding.Default.GetString(result);
button1.Enabled = true;
}
}
如果您有进一步的说明,请务必询问。
答案更新
感谢levent,我的做法是这样的
这是我的问题的完整答案:
string testurl = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json";
BackgroundWorker worker = new BackgroundWorker();
public Form2()
{
InitializeComponent();
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
}
private void button1_Click(object sender, EventArgs e)
{
SetEnabled(button1, false);
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
string url = testurl;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
try
{
while (!rdr.EndOfStream)
{
var str = rdr.ReadLine();
SetText(richTextBox1, str);
}
}
catch (Exception)
{
}
}
}
worker.CancelAsync();
SetEnabled(button1, true);
}
delegate void SetEnabledCallback(Button target, bool value);
private void SetEnabled(Button target, bool value)
{
if (target.InvokeRequired)
{
SetEnabledCallback d = new SetEnabledCallback(SetEnabled);
this.Invoke(d, new object[] { target, value });
}
else
{
target.Enabled = value;
}
}
delegate void SetTextCallback(dynamic target, string text);
private void SetText(dynamic target, string text)
{
if (target.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { target, text });
}
else
{
target.Text += text;
}
}
此代码读取消息,您可以运行它在异步操作中。
string url = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json%22link%22";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using(WebResponse response = request.GetResponse())
{
using(StreamReader rdr = new StreamReader(response.GetResponseStream())){
while(!rdr.EndOfStream){
var str = rdr.ReadLine();
Console.WriteLine(str);
}
}
}
我们有这个URL 连接不会关闭并继续每隔几秒左右无限期地发送响应。
我想获取部分数据,但使用我当前的代码,它会在显示数据之前等待连接结束或关闭,但由于该网页会无限期响应,我编写的程序也会无限期地等待。
但是对于其他测试 URL,例如 www.google.com 立即关闭连接,它工作正常。
这是我的代码:
public partial class Form2 : Form
{
string testurl = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json";
WebClient wc = new WebClient();
public Form2()
{
InitializeComponent();
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadComplete);
}
private void button1_Click(object sender, EventArgs e)
{
wc.DownloadDataAsync(new Uri(testurl));
button1.Enabled = false;
}
private void DownloadComplete(object sender, DownloadDataCompletedEventArgs args)
{
byte[] result = args.Result;
richTextBox1.Text += System.Text.Encoding.Default.GetString(result);
button1.Enabled = true;
}
}
如果您有进一步的说明,请务必询问。
答案更新 感谢levent,我的做法是这样的
这是我的问题的完整答案:
string testurl = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json";
BackgroundWorker worker = new BackgroundWorker();
public Form2()
{
InitializeComponent();
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
}
private void button1_Click(object sender, EventArgs e)
{
SetEnabled(button1, false);
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
string url = testurl;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
try
{
while (!rdr.EndOfStream)
{
var str = rdr.ReadLine();
SetText(richTextBox1, str);
}
}
catch (Exception)
{
}
}
}
worker.CancelAsync();
SetEnabled(button1, true);
}
delegate void SetEnabledCallback(Button target, bool value);
private void SetEnabled(Button target, bool value)
{
if (target.InvokeRequired)
{
SetEnabledCallback d = new SetEnabledCallback(SetEnabled);
this.Invoke(d, new object[] { target, value });
}
else
{
target.Enabled = value;
}
}
delegate void SetTextCallback(dynamic target, string text);
private void SetText(dynamic target, string text)
{
if (target.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { target, text });
}
else
{
target.Text += text;
}
}
此代码读取消息,您可以运行它在异步操作中。
string url = "https://live.wh.sportingpulseinternational.com/v1/basketball/read/448426?ak=6eea426648c256cae71a9409352304fe&types=scoreboard,status,boxscore&format=json%22link%22";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using(WebResponse response = request.GetResponse())
{
using(StreamReader rdr = new StreamReader(response.GetResponseStream())){
while(!rdr.EndOfStream){
var str = rdr.ReadLine();
Console.WriteLine(str);
}
}
}