使用此处提供的代码示例:https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110051-configuring-proxy 仅适用于HTTP(S(代理,而不适用于socks5。是否支持对 SOCKS 5 执行相同的操作?谢谢
DotNetBrowser基于Chromium引擎。不幸的是,Chromium引擎不支持SOCKS 5身份验证,因此DotNetBrowser也不支持此类身份验证。
Chromium错误跟踪器中存在一个问题:https://bugs.chromium.org/p/chromium/issues/detail?id=256785
一旦我们切换到支持SOCKS 5身份验证的Chromium版本,我们也会将此实现添加到DotNetBrowser中。
作为解决方法,您可以尝试创建自己的"IProtocolHandler"接口实现,以使用第三方库处理对所需代理服务器的请求。但是,请注意,请求无法并行处理,因此性能可能会显着降低。
以下文章演示如何注册自定义协议处理程序:https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000134504-custom-protocol-handler
根据尤金·雅库什的建议,我想出了这个:
public class HttpsHandler : IProtocolHandler
{
public string socksHost = string.Empty;
public int socksPort = -100;
public bool socksLogin = false;
public string socksUsername = string.Empty;
public string socksPassword = string.Empty;
//This method should provide the response for the specified request
private Chilkat.Http http;
private Chilkat.HttpRequest httpRequest;
private Chilkat.HttpResponse httpResponse;
private Chilkat.Spider spider;
public HttpsHandler()
{
this.http = new Chilkat.Http();
this.spider = new Chilkat.Spider();
}
public IUrlResponse Handle(IUrlRequest request)
{
http.SocksHostname = socksHost;
http.SocksPort = socksPort;
http.SocksVersion = 5;
if (socksLogin)
{
http.SocksUsername = socksUsername;
http.SocksPassword = socksPassword;
}
httpRequest = new Chilkat.HttpRequest();
httpRequest.HttpVerb = request.Method.ToUpper();
Console.WriteLine(request.Url);
if (request.Method.ToUpper() == "POST")
{
var xxl = request.PostData;
PostData post = request.PostData;
FormData postData = (FormData)post;
var keys = postData.GetPairKeys();
foreach (var item in keys)
{
var val = postData.GetPairValues(item);
Console.WriteLine(item + ":" + val[0]);
httpRequest.AddParam(item, val[0]);
}
}
var heads = request.Headers.GetHeaders();
foreach (KeyValuePair<string, List<string>> pair in heads)
{
httpRequest.AddHeader(pair.Key, pair.Value[0]);
Console.WriteLine(pair.Key);
Console.WriteLine(pair.Value[0]);
Console.WriteLine("===================");
}
string path = request.Url;
string domain = spider.GetUrlDomain(request.Url);
path = path.Replace("https://" + domain, string.Empty);
httpRequest.AddHeader("Host", domain);
httpRequest.Path = path;
httpResponse = http.SynchronousRequest(domain, 443, true, httpRequest);
Console.WriteLine(httpResponse.StatusCode);
if (httpResponse == null)
{
return new UrlResponse(Encoding.UTF8.GetBytes(string.Empty), (System.Net.HttpStatusCode)httpResponse.StatusCode);
}
return new UrlResponse(Encoding.UTF8.GetBytes(httpResponse.BodyStr), (System.Net.HttpStatusCode)httpResponse.StatusCode);
}
}
显然,它可以简化。但如果有人有同样的问题,这就是我的解决方案。