所以我有這個程序,使用一個簡短的鏈接(我使用谷歌URL縮短)獲取一個頁面。 要建立我的例子中我使用了Using WebClient in C# is there a way to get the URL of a site after being redirected?C#WebClient OpenRead url
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyWebClient client = new MyWebClient();
client.OpenRead("http://tinyurl.com/345yj7x");
Uri uri = client.ResponseUri;
Console.WriteLine(uri.AbsoluteUri);
Console.Read();
}
}
class MyWebClient : WebClient
{
Uri _responseUri;
public Uri ResponseUri
{
get { return _responseUri; }
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
_responseUri = response.ResponseUri;
return response;
}
}
}
代碼我不understant一件事:當我做這個client.OpenRead("http://tinyurl.com/345yj7x");
下載頁面的URL指向?如果這種方法下載頁面,我需要一些東西來讓我只有網址,所以如果有一種方法只獲得一些標題,或只有網址,請讓我知道。
舷窗。然後,只需使用response.ResponseUri來獲取您的網址 - 在這種情況下,它看起來是「http://www.google.ro/search?sourceid=chrome&ie=UTF-8&q=c%23+webclient+tinyurl」 – Reddog 2010-12-22 10:13:18