本文介绍了创建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("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")

这篇关于创建HttpClient之后,可以更改HttpClientHandler的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:07