创建HttpClient之后,可以更改HttpClientHandler的属性吗?

编程入门 行业动态 更新时间:2024-10-07 11:24:10
本文介绍了创建HttpClient之后,可以更改HttpClientHandler的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在创建HttpClient对象时,可以将HttpClientHandler用作参数,但是在此之后,似乎没有任何方法可以在不保留对它的引用的情况下访问该处理程序.

The HttpClientHandler can be used as a parameter when creating a HttpClient object, but after that there doesn't seem to be any way to access the handler without keeping a reference to it.

Dim Handler as New HttpClientHandler Handler.CookieContainer = Cookies Handler.Proxy = Proxy Handler.UseProxy = True Handler.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate Dim Client as New HttpClient(Handler, True)

我可以更改现有客户端对象的处理程序的属性吗?例如,更改代理或自动重定向.客户端当前正在处理其他HttpRequestMessages时,这样做是否会有任何问题?

Am I able to change the properties of a handler of an existing client object? For example, change the Proxy or the AutoRedirect. Would I have any issues doing this while other HttpRequestMessages are currently being processed by the client?

推荐答案

是的,可以.关键是更改对象而不是httpclient的属性.记住OOP 101.

Yes, you can. The key is to change the object and not the property of the httpclient. Remember OOP 101.

将属性指向相同的对象,但更改该对象的内容.

Point the property to the same object but change the contents of that object.

Dim Handler As New HttpClientHandler Dim proxy As New WebProxy() Dim urlBuilder As New System.UriBuilder Handler.Proxy = proxy Handler.UseProxy = True Handler.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate Dim Client As New HttpClient(Handler, True) urlBuilder.Host = "124.161.94.8" urlBuilder.Port = 80 proxy.Address = urlBuilder.Uri Dim response As String = Await Client.GetStringAsync("www.ipchicken") urlBuilder.Host = "183.207.228.8" urlBuilder.Port = 80 proxy.Address = urlBuilder.Uri response = Await Client.GetStringAsync("www.ipchicken")

更多推荐

创建HttpClient之后,可以更改HttpClientHandler的属性吗?

本文发布于:2023-11-12 14:28:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   HttpClient   HttpClientHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!