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

      

    运行的效果是这样的:

  • 相关阅读:
    K-Means++ 聚类之数据可视化:使用gnuplot
    QQ设计第1-5步
    QQ设计第1-5步
    为什么有很深的windows基础还是不能动摇linux半步
    常用命令
    在线会计_金蝶友商网
    XP使用VNC远程桌面CentOS 6
    Fatal error: Call to undefined function mb_substr()
    如何汉化 po 文件及编译成 mo 文件
    idoerp
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132868.html
Copyright © 2011-2022 走看看