C# 实现对微博短网址的重定向还原
新浪微博中,为了节省输入字数,通过短网址对发布微博中链接进行重定向。我们可以通过代码实现对短网址进行还原,代码如下:
private string GetOrignalLink(string link) { using (WebClient client = new MyWebClient()) { client.Headers.Add("Referer", link); Stream stream = null; try { stream = client.OpenRead(link); return client.ResponseHeaders["Location"]; } catch (Exception) { throw; } finally { if (stream != null) stream.Close(); } } } internal class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.AllowAutoRedirect = false; return request; } }