2014-09-11 20 views
0

HttpClientHandler는 HttpClient 객체를 만들 때 매개 변수로 사용할 수 있지만 그 이후에는 참조를 유지하지 않고 처리기에 액세스 할 수있는 방법이없는 것처럼 보입니다.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) 

기존 클라이언트 개체의 처리기 속성을 변경할 수 있습니까? 예를 들어 프록시 또는 AutoRedirect를 변경하십시오. 다른 HttpRequestMessages가 현재 클라이언트에 의해 처리되는 동안이 작업을 수행하는 데 문제가 있습니까?

+0

이 MSDN 설명서는 AutoRedirects에서 읽었습니까? [자동 리디렉션 허용] (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect (v = vs.110) .aspx) – MethodMan

+0

@DJKRAZE 감사하지만 사용하지는 않습니다. HttpWebRequest하지만 .NET 4.5의 HttpClient – iguanaman

답변

1

예, 가능합니다. 핵심은 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") 
+0

물론,이 방법을 동시 또는 다중 스레드 환경에서 사용하면 작동하지 않을 수도 있습니다. 그리고 말할 필요도없이, 비 객체 값 유형 속성은 변경 될 수 없습니다. – Kallol

1

HttpClientHandler의 속성을 수정하려고하면 추가 테스트를 수행 한 후 예외가 발생합니다. HttpClient의 새로운 인스턴스가 필요합니다.

관련 문제