[C#][Winform][ASP.net] 取得轉址的跳轉後網址(真實網址)

2012-10-05

 

透過 ASP.net 撰寫轉址網頁非常簡單..

只要執行..

Response.Redirect("http://www.bing.com");

就可以指定到專屬的頁面..


如果是在 Page_Load 執行..


protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("http://www.bing.com");
}

一進入頁面後就會進行跳轉..


這邊我有寫一個簡單的轉址網站..


2012-10-05_150116



有興趣的可以參考一下,因為是新網站所以跟 http://0rz.tw 還有 http://ppt.cc/ 比起來


暫時不會被 Facebook 給警告…有時候因為使用這些常見縮址所以 po 文都要再打一次認證碼..


說到重點…因為最近要做一些社群上面的分析..


在 Twitter 中 他門會把網址縮成這副德行 http://t.co/nh9Ognkd


這跳轉後的網址是 http://technet.tw


之後我透過 WebClient 去抓取 會出現 跳轉後的網頁內容..


WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
txtRealPathData.Text = client.DownloadString(new Uri(“http://t.co/nh9Ognkd));

結果:


sshot-203_2


果然是跳轉後的結果…我們用 Fiddler 看一下到底 http://t.co/nh9Ognkd 記錄了什麼


sshot-204_2



HTTP/1.1 200 OK
Date: Fri, 27 Apr 2012 06:58:52 GMT
Server: hi
Content-Type: text/html; charset=utf-8
Cache-Control: private,max-age=300
Expires: Fri, 27 Apr 2012 07:03:52 GMT
Vary: Accept-Encoding
Content-Length: 138
Connection: close
 
<noscript><META http-equiv="refresh" content="0;URL=http://technet.tw"></noscript><script>location.replace("http:\/\/technet.tw")</script>

 


這時候要如何抓出跳轉後的網址呢?1


其實.net framework 都很溫馨的做好了…



// 取得他到底倒底轉到那個網址
       WebRequest webRequest = WebRequest.Create(“http://t.co/nh9Ognkd);
       WebResponse webResponse = webRequest.GetResponse();
       this.txtRedirectPathContent.Text = (webResponse.ResponseUri.ToString());

執行結果:


sshot-205_2


下面有附上檔案有需要的就自取改寫..



當麻許的超技八 2014 | Donma Hsu Design.