我想解開網址,但一直沒能找到代碼(vb.net/c#)來做到這一點。這些是Twitter縮短的URL,我想我可以嘗試訪問可用的Web服務之一,並執行httpwebrequest,但希望找到一些編程方式來做到這一點。unshortening urls
回答
你可以直接從縮短網址的迴應中得到它,因爲它會返回一個狀態碼MovedPermanently
以及真實網址的位置(這對大多數網站都適用,不需要導航到真實網址)
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://t.co/xqbLEi6s");
req.AllowAutoRedirect = false;
var resp = req.GetResponse();
string realUrl = resp.Headers["Location"];
其他的測試數據:http://goo.gl/zdf2n
,http://tinyurl.com/8xc9vca
,http://x.co/iEup
,http://is.gd/vTOlz6
,http://bit.ly/FUA4YU
或者您可以允許自動重定向並從resp.ResponseUri屬性獲取真實URL: 'HttpWebRequest req =(HttpWebRequest)WebRequest.Create(「http://t.co/xqbLEi6s」); var resp = req.GetResponse(); string realUrl = resp.ResponseUri.AbsoluteUri;' – Yuriy 2013-10-04 08:35:01
@Yuriy爲什麼我應該加載整個(可能很大)的頁面,當我可以做一個小的響應。 – 2013-10-04 08:54:03
我還沒有想過,你是對的,你的代碼更有效率。 – Yuriy 2013-10-04 12:56:05
在沒有詢問創建URL的服務(並且每種服務的請求方式會不同)的情況下,或者更實用地說,只需打開URL並查看其重定向到的位置,沒有任何神奇的方法可以解除URL的釋放。
- 1. Django urls/slugs @#!
- 2. Java和SEO URLS
- 3. Rails URLs not working
- 4. php:SEO friendly urls
- 5. Nginx的Canonical URLs
- 6. geochart region clickable urls
- 7. Google Analytics for short urls
- 8. Joomla URL re-write urls
- 9. Foreach Urls JSON解碼
- 10. NoReverseMatch在/ <urls>
- 11. 清潔URLS /的.htaccess
- 12. 分享動態URLS
- 13. BlackBerry問題與urls
- 14. javascript中的Masking urls
- 15. Url Rewrite for angular urls
- 16. MediaWiki with short URLs and SSL
- 17. HTTP POST prarameters order/REST urls
- 18. Rewerite URLS的.htaccess主機
- 19. BeautifulSoup Absoute URLs Print to CSV
- 20. Get link_stats for many..MANY urls
- 21. Apache mod-rewrite for short urls
- 22. jQuery Mobile URLs未更新
- 23. Node.js和Mongoose - SEO Friendly URLs
- 24. tidhttp get procedure not working on urls
- 25. 可能的根本URLS
- 26. Spring Web Api,解析URLS
- 27. php array對於urls唯一
- 28. 的.htaccess:操縱/更改URLS
- 29. Django:ImportError at/No module named urls
- 30. 使用Ember.js的Hashbang URLs
如何使用'HttpWebRequest'訪問Web服務不是編程?你寫了一個程序來使用這個類,不是嗎? – BoltClock 2012-03-18 17:26:01
您採取的任何方法都必須涉及對某些外部API執行查詢。 – 2012-03-18 17:27:26