远程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; }