使用HttpClient获得Ur最终跳转页面信息

来源:互联网 发布:mac install ant 编辑:程序博客网 时间:2024/05/24 07:03
可以获取重定向或者通过 <meta refresh来跳转的最终地址,解决获取https型的url最终地址的问题
代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;using System.Text.RegularExpressions;namespace Infrastructure.Helper{    public class HttpClientHelper : IDisposable    {        private HttpClient httpClient;        private HttpClientHandler handler;        public HttpClientHelper()        {            InitHttpClient();        }        public HttpClientHelper(HttpClientHandler handler)        {            this.handler = handler;            InitHttpClient();        }        /// <summary>        /// Url检查结果类        /// </summary>        public class UrlReachableResult        {            public bool Success;            public string ErrorMsg;            public string RawUrl;            public string SourceUrl;            public string StatusCode;            public UrlReachableResult()            {                Success = false;                ErrorMsg = string.Empty;                RawUrl = string.Empty;                SourceUrl = string.Empty;                StatusCode = string.Empty;            }        }        private UrlReachableResult checkUrlCanReached(string checkUrl, int cycleIndex = 0)        {            var result = new UrlReachableResult();            result.SourceUrl = checkUrl;            HttpResponseMessage response = null;            try            {                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);                using (var task = httpClient.GetAsync(checkUrl, HttpCompletionOption.ResponseContentRead))                {                    task.Result.EnsureSuccessStatusCode();                    response = task.Result;                    result.StatusCode += response.StatusCode;                    result.RawUrl = response.RequestMessage.RequestUri.ToString();                    result.Success = true;                    var contentHtml = response.Content.ReadAsStringAsync().Result;                    var match = Regex.Match(contentHtml, "<meta[^>]+http-equiv=[\"']{0,1}refresh[\"']{0,1}[^>]*>");                    if (match.Success && !contentHtml.Contains("<script"))                    {                        var metaMatches = Regex.Match(match.Value, "content=\\s*[\"'][^\"']*url=([^\"';]*)");                        if (metaMatches.Success && metaMatches.Groups.Count >= 2 && !string.IsNullOrWhiteSpace(metaMatches.Groups[1].Value))                        {                            var innerResult = checkUrlCanReached(metaMatches.Groups[1].Value, ++cycleIndex);                            innerResult.SourceUrl = checkUrl;                            result = innerResult;                        }                    }                }            }            catch (HttpRequestException ex)            {                result.ErrorMsg = "无法打开链接地址!";            }            catch (Exception ex)            {                result.ErrorMsg = ex.ToString();            }            finally            {                if (response != null)                    response.Dispose();            }            return result;        }        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)        {            return true; //总是接受          }          private void InitHttpClient()        {            if (handler == null)            {                handler = new HttpClientHandler();                handler.AllowAutoRedirect = true;            }            httpClient = new HttpClient(handler);            //设置客户端可以接收的类型(接收所有) 如果不添加,请求有些服务器会报错            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));            // Limit the max buffer size for the response so we don't get overwhelmed            //httpClient.MaxResponseContentBufferSize = 256000;            // Add a user-agent header            httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");        }        public void Dispose()        {            if (httpClient != null)                httpClient.Dispose();        }    }}

0 0
原创粉丝点击