C# 搜狗链接网址转换为真实网址

一、问题:

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

  例:

    原链接:https://www.sogou.com/link?url=DOb0bgH2eKjRiy6S-EyBciCDFRTZxEJg9BcDwvTo02r26FDhCNBXEOt74PAtEBpptz5XqOoFKPCt5pUc3zy0dg..

    跳转后的链接:https://baike.sogou.com/v7677690.htm?fromTitle=text%28%29

这里我们需要得到跳转后的链接,此方法也可用于360搜索链接转换

二、代码

添加Nuget:HtmlAgilityPack程序包

引用:using HtmlAgilityPack;

        public static string ResultOfApi(string url)
        {
            //发送请求
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "Get";
            request.ContentType = "application / x - www - form - urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
            request.Accept = "text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng,*/*;q=0.8";
            request.KeepAlive = true;
            //获取响应
            response = (HttpWebResponse)request.GetResponse();
            Stream S = response.GetResponseStream();
            StreamReader streamreader = new StreamReader(S);
            string result = streamreader.ReadToEnd();
            response.Close();
            //加载源代码,获取文档对象
            var doc = new HtmlDocument(); doc.LoadHtml(result);
            string text_XPath = @"/script[1]";
            var text_list = doc.DocumentNode.SelectSingleNode(text_XPath);
            string newUrl = text_list.InnerHtml.Substring(text_list.InnerHtml.IndexOf('"') + 1);
            newUrl = newUrl.Remove(newUrl.LastIndexOf('"'));return newUrl;
        }
原文地址:https://www.cnblogs.com/duhaoran/p/14385536.html