• Http请求报头设置(C#)


    1、添加一个SetHeaderValue方法:

    public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
    {
    var property = typeof(WebHeaderCollection).GetProperty("InnerCollection",BindingFlags.Instance | BindingFlags.NonPublic);
    if (property != null)
    {
    var collection = property.GetValue(header, null) as NameValueCollection;
    collection[name] = value;
    }
    }

    2、调用这个方法设置HttpHeader头:

    HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("Url");
    rq.Method = "GET";
    SetHeaderValue(rq.Headers, "Host", "127.0.0.1");
    SetHeaderValue(rq.Headers, "Connection", "keep-alive");
    SetHeaderValue(rq.Headers, "Accept", "*/*");
    SetHeaderValue(rq.Headers, "X-Requested-With", "XMLHttpRequest");
    SetHeaderValue(rq.Headers, "User-Agent", "...");
    SetHeaderValue(rq.Headers, "Referer", "http://127.0.0.1/index.php?m=Index&a=indexs");
    SetHeaderValue(rq.Headers, "Accept-Encoding", "gzip, deflate");
    SetHeaderValue(rq.Headers, "Accept-Language", "1.5");
    SetHeaderValue(rq.Headers, "Cookie", "This is Cookie");
     
    HttpWebResponse resp = (HttpWebResponse)rq.GetResponse();
     
    using (Stream stream = resp.GetResponseStream())
    {
    StreamReader reader =new StreamReader(stream,Encoding.Default);
    string responseString = reader.ReadToEnd();
    MessageBox.Show(responseString);
    }

    3.可以通过抓包软件(如SRSniffer)查看到设置的报头。

    来源;https://www.cnblogs.com/hnsongbiao/p/9381987.html

  • 相关阅读:
    周练2
    周练1
    周赛6(28)
    django中的orm:
    crm项目包含django创建虚拟环境:
    crm项目建表(django自带认证、分页、插件功能):
    自动化测试js代码打印类名:
    pages框架之豆瓣:
    mybatis反向生成实体类、dao层以及映射文件
    mybatis反向生成实体类、dao层以及映射文件
  • 原文地址:https://www.cnblogs.com/sharing1986687846/p/10282527.html
Copyright © 2020-2023  润新知