• [C#]Http请求报头设置


    https://blog.csdn.net/chordwang/article/details/54311033

    1.添加一个SetHeaderValue方法:

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

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

    1. HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("Url");
    2. rq.Method = "GET";
    3. SetHeaderValue(rq.Headers, "Host", "127.0.0.1");
    4. SetHeaderValue(rq.Headers, "Connection", "keep-alive");
    5. SetHeaderValue(rq.Headers, "Accept", "*/*");
    6. SetHeaderValue(rq.Headers, "X-Requested-With", "XMLHttpRequest");
    7. SetHeaderValue(rq.Headers, "User-Agent", "...");
    8. SetHeaderValue(rq.Headers, "Referer", "http://127.0.0.1/index.php?m=Index&a=indexs");
    9. SetHeaderValue(rq.Headers, "Accept-Encoding", "gzip, deflate");
    10. SetHeaderValue(rq.Headers, "Accept-Language", "1.5");
    11. SetHeaderValue(rq.Headers, "Cookie", "This is Cookie");
    12. HttpWebResponse resp = (HttpWebResponse)rq.GetResponse();
    13. using (Stream stream = resp.GetResponseStream())
    14. {
    15. StreamReader reader =new StreamReader(stream,Encoding.Default);
    16. string responseString = reader.ReadToEnd();
    17. MessageBox.Show(responseString);
    18. }
    3.可以通过抓包软件(如SRSniffer)查看到设置的报头.

  • 相关阅读:
    java把集合数据写入txt文档
    eclipse 启动报内存溢出的问题out of memory!
    面向对象基础知识整理
    在不同环境下MD5加密相同字符串,密文不一样的问题
    氮化镓选型
    碳化硅选型
    DSP选型
    FPGA选型
    MCU选型
    正点原子
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14608570.html
Copyright © 2020-2023  润新知