C#使用HttpClient获取Location

之前使用HttpWebRequest的时候,只需要设置HttpWebRequest对象的AllowAutoRedirect属性值为false即可在Respomse的Header中获取Location;

但是后来在SilverLight中使用HttpWebRequest的时候,没有AllowAutoRedirect这个属性了,各种点也点不出来,于是只能用HttpClient 了,代码如下:

1 public string GetLocation(string URL)
2         {
3             HttpClientHandler hander = new HttpClientHandler();
4             hander.AllowAutoRedirect = false;
5             HttpClient client = new HttpClient(hander);
6             return client.GetAsync(URL).Result.Headers.Location.ToString();
7         }

引用名词空间System.Net.Http

posted @ 2017-02-19 09:15  CherishYou  阅读(2305)  评论(0编辑  收藏  举报