-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientX.cs
107 lines (94 loc) · 3.13 KB
/
HttpClientX.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace wform_v3.Models
{
public static class HttpClientX
{
private static object _locker = new object();
private static List<BoxIWebDriver> _drivers = new List<BoxIWebDriver>();
public static void Close()
{
foreach(var d in _drivers)
{
try
{
d.Driver.Quit();
}
catch
{
}
}
}
public static async Task Init()
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
tasks.Add(
Task.Run(() =>
{
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddArguments(new List<string>() { "headless", "disable-gpu" });
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
driver = new ChromeDriver(chromeDriverService, options);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
BoxIWebDriver box = new BoxIWebDriver(driver);
_drivers.Add(box);
}));
}
await Task.WhenAll(tasks);
}
public static async Task<string> Find(string url)
{
BoxIWebDriver box;
string resp = string.Empty;
await Task.Run(() => {
while (true)
lock (_locker)
{
box = _drivers.FirstOrDefault(x => x.Free == true);
if (box != null)
{
box.Free = false;
break;
}
else
Thread.Sleep(1000);
}
IWebDriver driver = box.Driver;
Actions action = new Actions(driver);
try
{
driver.Navigate().GoToUrl(url);
action.MoveByOffset(5, 5).Perform();
action.MoveByOffset(10, 15).Perform();
action.MoveByOffset(20, 15).Perform();
resp = driver.PageSource;
}
catch
{
}
box.Free = true;
});
return resp;
}
class BoxIWebDriver
{
public IWebDriver Driver;
public bool Free = true;
public BoxIWebDriver(IWebDriver driver)
{
Driver = driver;
}
}
}
}