[英]How can i use with proxy?
I'm new at anglesharp. 我是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.
现在,我正在使用它,它可以与webclient一起很好地工作,但是不能与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.
我正在获取原始IP,而不是代理。 But with WebClient it works well.
但是使用WebClient可以很好地工作。
You just add something ( handler
, ie, a HttpClientHandler
instance) to AngleSharp's configuration - something that will not be used by anything in AngleSharp. 您只需在AngleSharp的配置中添加一些内容(
handler
,即HttpClientHandler
实例),即可在AngleSharp中使用。
First of all AngleSharp's internal HTTP client is only a default client. 首先,AngleSharp的内部HTTP客户端仅是默认客户端。 For compatibility reasons AngleSharp cannot ship with
HttpClient
instead if uses HttpWebRequest
. 出于兼容性原因,如果使用
HttpWebRequest
则AngleSharp不能随HttpClient
一起提供。 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 ). 现在,如果您想使用代码,建议您使用AngleSharp.Io( https://www.nuget.org/packages/AngleSharp.Io或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. 仅添加
.WithRequesters(handler)
。 This adds the requesters from AngleSharp.Io. 这将添加来自AngleSharp.Io的请求者。 By providing
handler
we can configure the HttpClient
. 通过提供
handler
我们可以配置HttpClient
。
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.