c# - How can i use with proxy?
问题描述
I'm new at anglesharp. I tried the proxy change at the documentation but it didnt work. Now i'm using this, it works well with webclient but it isnt working with AngleSharp.
The code i'm trying is ;
var handler = new HttpClientHandler()
{
Proxy = new WebProxy(String.Format("{0}:{1}", "myProxy", "myPort"), false),
PreAuthenticate = true,
UseDefaultCredentials = false,
};
var config = Configuration.Default.WithJs().WithCookies().WithDefaultLoader().With(handler);
//Create a new context for evaluating webpages with the given config
var context = BrowsingContext.New(config);
var document = await context.OpenAsync("https://api.ipify.org?format=json");
Console.WriteLine(document.DocumentElement.OuterHtml);
I'm not getting any error, proxy is not working thats it. I'm getting my original ip not the proxys. But with WebClient it works well.
解决方案
You just add something (handler
, i.e., a HttpClientHandler
instance) to AngleSharp's configuration - something that will not be used by anything in AngleSharp.
First of all AngleSharp's internal HTTP client is only a default client. For compatibility reasons AngleSharp cannot ship with HttpClient
instead if uses HttpWebRequest
. This also allows you setting a proxy.
Now if you want to use your code I suggest you use AngleSharp.Io (https://www.nuget.org/packages/AngleSharp.Io or https://github.com/AngleSharp/AngleSharp.Io). It's quite simple and straight forward:
var handler = new HttpClientHandler
{
Proxy = new WebProxy(String.Format("{0}:{1}", "myProxy", "myPort"), false),
PreAuthenticate = true,
UseDefaultCredentials = false,
};
var config = Configuration.Default
.WithRequesters(handler)
.WithDefaultLoader()
.WithJs()
.WithTemporaryCookies()
.WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync("https://api.ipify.org?format=json");
Console.WriteLine(document.DocumentElement.OuterHtml)
Only .WithRequesters(handler)
has been added. This adds the requesters from AngleSharp.Io. By providing handler
we can configure the HttpClient
.
Hope that helps!
推荐阅读
- reactjs - 如何使用 reactjs 创建大型菜单
- r - 需要帮助通过顺序消除数据来重新采样 R 中的观察值
- r - 在 R 中比较具有不同时间戳的时间序列
- django - 如何在 django admin 中更改此特定文本输出?
- autohotkey - IF 循环之一无法正常工作,我不知道为什么
- swift - AudioKit v5:节拍器示例
- python - 如何通过代理使用 Boto3 S3 连接?
- phpstorm - 为什么我的 PHP 文件类被识别为文本文件?
- rabbitmq - RabbitMQ - 更改交换绑定
- php - 无法让 WP Customizer Color Picker 在我的网站上实际更改颜色