您的位置:首页 > 编程语言 > C#

C# 实现对微博短网址的重定向还原

2013-07-24 09:46 204 查看
新浪微博中,为了节省输入字数,通过短网址对发布微博中链接进行重定向。我们可以通过代码实现对短网址进行还原,代码如下:

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: