HttpWebRequest 使用示例:
public partial class Form1 : Form
{
private HttpWebRequest request;
private HttpWebResponse response;
public Form1()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
try
{
request = (HttpWebRequest)HttpWebRequest.Create(txtUrl.Text);
response = (HttpWebResponse)request.GetResponse();
txtResponse.Text="";
txtResponse.Text += "CharacterSet="+response.CharacterSet+"/r/n";
txtResponse.Text += "ContentEncoding=" + response.ContentEncoding + "/r/n";
txtResponse.Text += "ContentType=" + response.ContentType + "/r/n";
txtResponse.Text += "ContentLength=" + response.ContentLength + "/r/n";
txtResponse.Text += "ProtocolVersion=" + response.ProtocolVersion.Major + "." + response.ProtocolVersion.Minor + "/r/n";
txtResponse.Text += "ResponseUri=" + response.ResponseUri.AbsoluteUri + "/r/n";
txtResponse.Text += "Server=" + response.Server + "/r/n";
txtResponse.Text += "StatusDescription=" + response.StatusDescription + "/r/n";
txtResponse.Text += "**********Headers**********/r/n";
//WebHeaderCollection headers=response.Headers;
for(int i=0;i<response.Headers.Count;i++)
{
txtResponse.Text += "/t"+response.Headers.Keys[i]+"=" + response.Headers[i] + "/r/n";
}
txtResponse.Text += "**********Cookies**********/r/n";
for(int i=0;i<response.Cookies.Count;i++)
{
txtResponse.Text += "/tCookies["+i+"]=" + response.Cookies[i] + "/r/n";
}
txtResponse.Text += "**********Content**********/r/n";
Stream stream=response.GetResponseStream();
StreamReader readStream = new StreamReader(stream, Encoding.UTF8);
txtResponse.Text += readStream.ReadToEnd();
webBrowser.Url = response.ResponseUri;
}
catch(System.UriFormatException)
{
txtResponse.Text = "无效的URL";
}
}
}
运行的效果是这样的: