创建HttpClient对象时,可以将HttpClientHandler用作参数,但是在此之后,似乎没有任何方法可以在不保留对它的引用的情况下访问该处理程序。
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时,这样做是否会有任何问题?
是的你可以。关键是更改对象而不是httpclient的属性。记住OOP 101。
将属性指向相同的对象,但更改该对象的内容。
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("http://www.ipchicken.com")
urlBuilder.Host = "183.207.228.8"
urlBuilder.Port = 80
proxy.Address = urlBuilder.Uri
response = Await Client.GetStringAsync("http://www.ipchicken.com")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句