zoukankan      html  css  js  c++  java
  • C# HttpWebRequest 使用,一个Http分析Demo

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

      

    运行的效果是这样的:

  • 相关阅读:
    D. Babaei and Birthday Cake--- Codeforces Round #343 (Div. 2)
    Vijos P1389婚礼上的小杉
    AIM Tech Round (Div. 2) C. Graph and String
    HDU 5627Clarke and MST
    bzoj 3332 旧试题
    codeforces 842C Ilya And The Tree
    codesforces 671D Roads in Yusland
    Travelling
    codeforces 606C Sorting Railway Cars
    codeforces 651C Watchmen
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132868.html
Copyright © 2011-2022 走看看