简体   繁体   English

如何与代理一起使用?

[英]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.Iohttps://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.

相关问题 我可以使用Linfu动态代理使用通用参数代理接口吗? - Can I use Linfu dynamic proxy to proxy an interface with generic parameters? 如何设置可用于测试与Http请求相关的功能的Web代理? - How do I set up a Web Proxy that I can use to test functionality related to Http Requests? 如何在RedditSharp中使用代理? - How could I use a Proxy with RedditSharp? 如何强制VM使用C#中的主机代理设置? - How can I force VM's to use the host proxy settings from C#? 如何让WebClient(Web服务客户端)自动使用默认代理服务器? - How can I get WebClient (webservice client) to automatically use the default proxy server? 在ajax-pro中,可以使用自定义类作为代理而不是system.web.ui.page吗? 如何? - In ajax-pro Can I use custom class as proxy instead of system.web.ui.page? and How? 如何将动态代理转换为POCO? - How can I convert a dynamic proxy into a POCO? 如何连接和断开代理? - How can I connect and disconnect from a proxy? 我可以使用WCF客户端代理直接替代SoapHttpClientProtocol吗? - Can I use a WCF client proxy as a straight replacement for SoapHttpClientProtocol? 我如何在Webkit-Sharp iMonoDevelop中使用代理 - How do I use proxy in webkit-sharp i nMonoDevelop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM