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

      

    运行的效果是这样的:

  • 相关阅读:
    nginx防盗链配置
    nginx禁止非sever_name指定域名访问
    linux下配置python环境 django创建helloworld项目
    node解析修改ngix配置文件
    ~/.ssh/config文件的使用
    SpringCloud-Feign声明式服务调用
    Hystrix 配置参数全解析
    Eureka 的高级使用
    eureka中显示有服务但是通过ribbon调用显示No instances available for service-hello的问题
    EureKa:服务注册与发现
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132868.html
Copyright © 2011-2022 走看看