C# 百度链接网址转换为真实网址

一、问题:

当我们拿到百度某个链接时,显示的是一个链接http://www.baidu.com/link?url=xxx,跳转后的链接是:https://jingyan.baidu.com/xxx

  例:

    原链接:http://www.baidu.com/link?url=o5-CyJpFk1D7uqLk2j_JUZj_c21WRHxBriq7joicttffJPJko5CvpciHQRsaUAb1Zufgc_9Ddy1AJ-CIiKFvRpfRIFvRBjRb9sFFmpOjmKC

    跳转后的链接:https://jingyan.baidu.com/article/b907e627cd7ccf46e6891c58.html

这里我们需要得到跳转后的链接

二、代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Study3
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.baidu.com/link?url=JlULc4CHuEv66CucFPeIBun-7xlXrmBVuFLsD-ZL8Q8tQVCw0LltiznnFYHXuhQD33x5lKp5bGY85Xx-WFHtvq");
            req.Method = "HEAD";
            req.AllowAutoRedirect = false;
            HttpWebResponse myResp = (HttpWebResponse)req.GetResponse();
            if (myResp.StatusCode == HttpStatusCode.Redirect)
            {
                Console.Write("redirected to:" + myResp.GetResponseHeader("Location"));
                myResp.Close();//释放资源不然会超时
            }
            Console.ReadKey();
        }
    }
}

原文地址:https://www.cnblogs.com/duhaoran/p/14367511.html