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

      

    运行的效果是这样的:

  • 相关阅读:
    Internet Explorer 安全区域注册表项说明
    Android强制设置横屏或竖屏
    SQLite to Asp.net Entity Framework 部署问题
    获取地里位置信息
    通信API、使用Web Workers处理线程
    本地存储、离线应用程序
    多媒体播放
    绘制图形
    表单与文件
    HTML5的结构
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132868.html
Copyright © 2011-2022 走看看