简体   繁体   中英

Set proxy server specific to a service request HTTP client+REST

using (var client = new System.Net.Http.HttpClient())
            {
                var response = client.GetAsync(fullUrl).Result;
}

I am creating HTTP client as above to consume a RESTfull service. I should be able to set proxy for this service request. How can I set proxy server specific to this service request only?

System.Net.Http.HttpClient没有TransportSettings属性,关于Microsoft.Http汇编的信息未知。

uSING Microsoft.Http I can

 HttpClient client = new HttpClient();
                /*Set Credentials to authenticate proxy*/
                client.TransportSettings.Proxy = new WebProxy(proxyAddress);
                client.TransportSettings.Proxy.Credentials = CredentialCache.DefaultCredentials;
                client.TransportSettings.Credentials = CredentialCache.DefaultCredentials;

                client.BaseAddress = new Uri(this.baseUrl);
                var response = client.Get(fullUrl);

                var jsonResponce = response.Content.ReadAsJsonDataContract<mYResponseoBJECT>();


 public static T ReadAsJsonDataContract<T>(this  HttpContent content)
        {
            return (T)content.ReadAsJsonDataContract<T>(new DataContractJsonSerializer(typeof(T)));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM