zhaolei
8 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
using System.Linq;
 
namespace Prow.Web
{
    /// <summary>
    /// IP138 网站地理位置服务实现类
    /// </summary>
    internal class BaiDuIp138Locator : DefaultIPLocator
    {
        /// <summary>
        /// 获得/设置 服务状态
        /// </summary>
        public string Status { get; set; } = "";
 
        /// <summary>
        /// 获得/设置 地理位置结果 
        /// </summary>
        public IEnumerable<BaiDuIp138LocatorResult> Data { get; set; } = new BaiDuIp138LocatorResult[0];
 
        /// <summary>
        /// 定位方法
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public override string Locate(IPLocatorOption option)
        {
            var location = Locate<BaiDuIp138Locator>(option);
            if (string.IsNullOrEmpty(location) && option.HttpClient != null)
            {
                // 使用百度查询 IP 地址
                option.HttpClient.DefaultRequestHeaders.Accept.Clear();
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html", 0.9));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xhtml+xml", 0.9));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml", 0.9));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/webp", 0.8));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/apng", 0.8));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*", 0.8));
                option.HttpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/signed-exchange", 0.9));
 
                var task = option.HttpClient.GetStringAsync($"http://www.baidu.com/s?wd={option.IP}");
                task.Wait(option.RequestTimeout);
 
                option.HttpClient.DefaultRequestHeaders.Accept.Clear();
                if (task.IsCompleted && !task.IsFaulted && task.Result != null)
                {
                    var content = task.Result ?? string.Empty;
                    var markString = $"<span class=\"c-gap-right\">IP地址:&nbsp;{option.IP}</span>";
                    var start = content.IndexOf(markString);
                    if (start > -1)
                    {
                        start += markString.Length;
                        content = content.Substring(start);
                    }
                    markString = "</td>";
                    start = content.IndexOf(markString);
                    if (start > -1)
                    {
                        content = content.Substring(0, start);
                        location = content.TrimEnd('\t', ' ', '\n', '\r');
                    }
                }
            }
            return location;
        }
 
        /// <summary>
        /// ToString 方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return (Status != "0" && Data.Any()) ? "XX XX" : Data.First()?.ToString() ?? "XX XX";
        }
    }
}