遠程URL:https://121.199.16.229:8890/generate.cgi?rbid=1001&esn=22021434025005&pic=png&count=1&pix=250&cor=lo&comm=%E6%B5%A6%E6%B1%9F%E6%B8%B8%E8%A7%881%E5%8F%B7%E9%BE%99%E8%88%B9%2F%E5%B7%A5%E8%A1%8C%E5%8F%B7%EF%BC%8C2013%E5%B9%B46%E6%9C%881%E5%8F%B7%EF%BD%9E2013%E5%B9%B46%E6%9C%8831%E5%8F%B7%EF%BC%8C14%3A00
執行后:會生成一個圖片地址 如https://121.199.16.229:8890/cgi/1001313097265699.png
怎么用程序 通過 內部訪問 遠程URL 獲得這個 圖片的URL
回答:
HttpWebRequest設置AutoRedirection就可以了。它會自動處理301跳轉。
添加System.Net和System.Security兩個dll,引入相應命名空間。
static void Main(string[] args) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://121.199.16.229:8890/generate.cgi?rbid=1001&esn=22021434025005&pic=png&count=1&pix=250&cor=lo&comm=%E6%B5%A6%E6%B1%9F%E6%B8%B8%E8%A7%881%E5%8F%B7%E9%BE%99%E8%88%B9%2F%E5%B7%A5%E8%A1%8C%E5%8F%B7%EF%BC%8C2013%E5%B9%B46%E6%9C%881%E5%8F%B7%EF%BD%9E2013%E5%B9%B46%E6%9C%8831%E5%8F%B7%EF%BC%8C14%3A00"); ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//驗證服務器證書回調自動驗證 WebResponse webResponse = webRequest.GetResponse(); Console.WriteLine(webResponse.ResponseUri.AbsoluteUri);//輸出https://121.199.16.229:8890/cgi/1001363993292848.png } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 總是接受 return true; }