private static ProxyClient GetProxyClient(Proxy proxy, HttpOptions options) { ProxyClient client; if (proxy == null) { client = new NoProxyClient(new ProxySettings()); } else { var settings = new ProxySettings() { Host = proxy.Host, Port = proxy.Port, ConnectTimeout = options.ConnectTimeout, ReadWriteTimeOut = options.ReadWriteTimeout }; if (proxy.NeedsAuthentication) { settings.Credentials = new NetworkCredential(proxy.Username, proxy.Password); } client = proxy.Type switch { ProxyType.Http => new HttpProxyClient(settings), ProxyType.Socks4 => new Socks4ProxyClient(settings), ProxyType.Socks4a => new Socks4aProxyClient(settings), ProxyType.Socks5 => new Socks5ProxyClient(settings), _ => throw new NotImplementedException() }; } return(client); }
public async Task SendAsync_Get_Cookies() { var name = "name"; var value = "value"; var message = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri($"http://httpbin.org/cookies/set?{name}={value}") }; var settings = new ProxySettings(); var proxyClient = new NoProxyClient(settings); var cookieContainer = new CookieContainer(); using var proxyClientHandler = new ProxyClientHandler(proxyClient) { CookieContainer = cookieContainer }; using var client = new HttpClient(proxyClientHandler); var response = await client.SendAsync(message); var cookies = cookieContainer.GetCookies(new Uri("http://httpbin.org/")); Assert.Single(cookies); var cookie = cookies[name]; Assert.Equal(name, cookie.Name); Assert.Equal(value, cookie.Value); client.Dispose(); }
private static async Task <HttpResponse> RequestAsync(HttpRequest request) { var settings = new ProxySettings(); var proxyClient = new NoProxyClient(settings); using var client = new RLHttpClient(proxyClient); return(await client.SendAsync(request)); }
public async Task ConnectAsync_NoProxyClient_Http() { var settings = new ProxySettings(); var proxy = new NoProxyClient(settings); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); var client = await proxy.ConnectAsync("example.com", 80, null, cts.Token); var response = await GetResponseAsync(client, BuildSampleGetRequest(), cts.Token); Assert.Contains("Example Domain", response); }
private static async Task <HttpResponseMessage> RequestAsync(HttpRequestMessage request) { var settings = new ProxySettings(); var proxyClient = new NoProxyClient(settings); using var proxyClientHandler = new ProxyClientHandler(proxyClient) { CookieContainer = new CookieContainer() }; using var client = new HttpClient(proxyClientHandler); return(await client.SendAsync(request)); }
/// <summary> /// Creates a socket that talks to the given <paramref name="host"/> on the given <paramref name="port"/> /// (optionally through a proxy) and returns the <see cref="NetworkStream"/>. /// </summary> public static async Task <TcpClient> GetClientAsync(string host, int port, TimeSpan timeout, Proxy proxy = null, CancellationToken cancellationToken = default) { ProxyClient client; if (proxy == null) { client = new NoProxyClient(new ProxySettings()); } else { var settings = new ProxySettings() { Host = proxy.Host, Port = proxy.Port, ConnectTimeout = timeout, ReadWriteTimeOut = timeout }; if (proxy.NeedsAuthentication) { settings.Credentials = new NetworkCredential(proxy.Username, proxy.Password); } client = proxy.Type switch { ProxyType.Http => new HttpProxyClient(settings), ProxyType.Socks4 => new Socks4ProxyClient(settings), ProxyType.Socks4a => new Socks4aProxyClient(settings), ProxyType.Socks5 => new Socks5ProxyClient(settings), _ => throw new NotImplementedException() }; } return(await client.ConnectAsync(host, port, null, cancellationToken)); } }
public async Task SendAsync_Get_Cookies() { var name = "name"; var value = "value"; var cookies = new Dictionary <string, string>(); var message = new HttpRequest { Method = HttpMethod.Get, Uri = new Uri($"http://httpbin.org/cookies/set?{name}={value}"), Cookies = cookies }; var settings = new ProxySettings(); var proxyClient = new NoProxyClient(settings); using var client = new RLHttpClient(proxyClient); var response = await client.SendAsync(message); Assert.Single(cookies); Assert.Equal(value, cookies[name]); }