.net 4.0 以下HttpWebRequest Header 修改hosts方法
.net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录
1 public class CusteredHeaderCollection : WebHeaderCollection 2 { 3 public bool HostHeaderValueReplaced { get; private set; } 4 5 public string ClusterUrl { get; private set; } 6 7 public CusteredHeaderCollection(string commonClusterUrl) : base() 8 { 9 if (string.IsNullOrEmpty("commonClusterUrl")) 10 throw new ArgumentNullException("commonClusterUrl"); 11 12 this.ClusterUrl = commonClusterUrl; 13 } 14 15 public override string ToString() 16 { 17 this["Host"] = this.ClusterUrl; 18 string tmp = base.ToString(); 19 this.HostHeaderValueReplaced = true; 20 21 return tmp; 22 } 23 24 } 25 26 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 27 28 FieldInfo headersFieldInfo = request.GetType().GetField("_HttpRequestHeaders", System.Reflection.BindingFlags.NonPublic 29 | System.Reflection.BindingFlags.Instance 30 | System.Reflection.BindingFlags.GetField); 31 32 CusteredHeaderCollection WssHeaders = new CusteredHeaderCollection(sDomain); 33 34 headersFieldInfo.SetValue(request, WssHeaders); 35 36 request.Proxy = null;