using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
WebHeaderCollection whc = wrs.Headers;
MethodInvoker mi=new MethodInvoker()
for (int i = 0; i < whc.Count; i++)
{
listBox1.Items.Add("Header"+ whc.GetKey(i)+" : " +whc[i]);
}
}
}
}
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
WebHeaderCollection whc = wrs.Headers;
MethodInvoker mi=new MethodInvoker()
for (int i = 0; i < whc.Count; i++)
{
listBox1.Items.Add("Header"+ whc.GetKey(i)+" : " +whc[i]);
}
}
}
}
改为这样,就可以实现对主线程listbox的操作。
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
{
public partial class Form1 : Form
{
WebHeaderCollection whc;
public Form1()
{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
whc = wrs.Headers;
MethodInvoker mi = new MethodInvoker(controla);
this.BeginInvoke(mi);
}
private void controla()
{
for (int i = 0; i < whc.Count; i++)
{
listBox1.Items.Add("Header" + whc.GetKey(i) + " : " + whc[i]);
}
}
}
}
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
{
public partial class Form1 : Form
{
WebHeaderCollection whc;
public Form1()
{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
whc = wrs.Headers;
MethodInvoker mi = new MethodInvoker(controla);
this.BeginInvoke(mi);
}
private void controla()
{
for (int i = 0; i < whc.Count; i++)
{
listBox1.Items.Add("Header" + whc.GetKey(i) + " : " + whc[i]);
}
}
}
}