在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论