.netcore3.0中,如何设置httpclient的代理IP

日期: 2020-01-13 10:48

可以通过ConfigureServices方法中

services.AddHttpClient("unicom", client =>

{

client.DefaultRequestHeaders.Add("Accept","text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, /; q=0.01");

client.DefaultRequestHeaders.Add("Accept-Encoding","gzip, deflate");

client.DefaultRequestHeaders.Add("Accept-Language","zh-CN,zh;q=0.9");

client.DefaultRequestHeaders.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");

}).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler

{

Proxy =new WebProxy($"http://{ip}:{por

也可以通过实例化HttpClientHandler的方法实现

var httpClientHandler=new HttpClientHandler()

{

Proxy = new WebProxy($"http://{ip}:{port}")

};

var httpClient=new HttpClient(httpClientHandler);

httpClient.GetStringAsync("http://www.baidu.com");

du.com");

现在的问题是, httpclient已被注入到某个service中, 在这个service中如果要实现动态的代理IP主要怎么去实现?

可以理解为每个http请求后需要更换一个ip以及对应的port

相关新闻