使用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
- 使用HttpClient获得Ur最终跳转页面信息
- 如果获得页面跳转的最终URL
- 获得最终CU深度信息
- httpclient处理页面跳转
- 使用HttpClient登录知乎获取返回页面信息
- 跳转网络页面信息
- 使用HttpClient获得网页内容
- Java 使用HttpClient发送GET请求,并获得返回的xml信息
- selenium 获得页面cookie信息
- frame页面跳转和信息提示页面
- frame页面跳转和信息提示页面
- 使用action页面跳转
- 使用HttpClient获得手机归属地
- C#中Hashtable 的使用,最终实现书签跳转功能
- HttpClient抓取【需登录跳转页面】的数据
- Android中页面跳转,并传递信息
- 跳转页面时,带提示信息
- yii2 跳转页面带提示信息
- hadoop集群搭建和测试
- iOS 合并使用多个Storyboard
- 整数划分
- Python 参考手册
- IOS简单画图
- 使用HttpClient获得Ur最终跳转页面信息
- C# 利用反射调用类下的方法
- Python2 转义字符
- CSS定位学习笔记
- C语言进行二进制文件的读写操作(fopen,fwrite,fread)
- TexturePacker获得免费key 的方法
- 最近写数据导出时碰到的问题
- 程序的创建于虚拟地址的关系
- 51nod 1094 和为k的连续区间(暴力和map优化)