Using a proxy with .NET 4.5 HttpClientc# Accessing API via system's proxy'UTF8' is not a supported encoding nameC# Web API - Internal Server Error 500Use Proxy in C# Portable Class Library (Microsoft.Net.Http)TCP connections are stacked until 65k when using HttpWebRequest fetching function - internet connectivity is lostSending email in .NET through GmailHow to escape braces (curly brackets) in a format string in .NET.NET String.Format() to add commas in thousands place for a numberDifference between decimal, float and double in .NET?How can I get the application's path in a .NET console application?Reading settings from app.config or web.config in .netNew HttpClient proxy settings issuesRetargeting solution from .Net 4.0 to 4.5 - how to retarget the NuGet packages?Pass windows identity to web api layerCookieContainer memory leak

Designing a magic-compatible polearm

Is there a term for the belief that "if it's legal, it's moral"?

Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?

How does DC work with natural 20?

I just entered the USA without passport control at Atlanta airport

Greeting with "Ho"

How can a warlock learn from a spellbook?

Counterfeit checks were created for my account. How does this type of fraud work?

What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?

How do internally carried IR missiles acquire a lock?

What does it cost to buy a tavern?

Dates on degrees don’t make sense – will people care?

Subtract the Folded Matrix

QGIS. Polygon doesn't repeat itself

Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?

What happened to Hopper's girlfriend in season one?

FD Battery Stations... How Do You Log?

What are the current battlegrounds for people’s “rights” in the UK?

What is the meaning of "понаехать"?

Is the specular reflection on a polished gold sphere white or gold in colour?

Mathematically modelling RC circuit with a linear input

Why don't countries like Japan just print more money?

How much steel armor can you wear and still be able to swim?

Rejecting an offer after accepting it just 10 days from date of joining



Using a proxy with .NET 4.5 HttpClient


c# Accessing API via system's proxy'UTF8' is not a supported encoding nameC# Web API - Internal Server Error 500Use Proxy in C# Portable Class Library (Microsoft.Net.Http)TCP connections are stacked until 65k when using HttpWebRequest fetching function - internet connectivity is lostSending email in .NET through GmailHow to escape braces (curly brackets) in a format string in .NET.NET String.Format() to add commas in thousands place for a numberDifference between decimal, float and double in .NET?How can I get the application's path in a .NET console application?Reading settings from app.config or web.config in .netNew HttpClient proxy settings issuesRetargeting solution from .Net 4.0 to 4.5 - how to retarget the NuGet packages?Pass windows identity to web api layerCookieContainer memory leak






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








50















I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.



Here's how I create the client:



private HttpClient CreateHttpClient(CommandContext ctx, string sid) 
var cookies = new CookieContainer();

var handler = new HttpClientHandler
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false,
Proxy = new WebProxy("http://localhost:8888", false, new string[]),
UseProxy = true,
;

// snip out some irrelevant setting of authentication cookies

var client = new HttpClient(handler)
BaseAddress = _prefServerBaseUrl,
;

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

return client;



then I send the request by:



var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;


Request goes straight to the server without attempting to hit the proxy. What did I miss?










share|improve this question
























  • Is your service under test on your local machine? Consider proxy bypassing for local addresses.

    – Xaqron
    May 13 '13 at 16:33












  • By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

    – NathanAldenSr
    May 13 '16 at 2:07


















50















I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.



Here's how I create the client:



private HttpClient CreateHttpClient(CommandContext ctx, string sid) 
var cookies = new CookieContainer();

var handler = new HttpClientHandler
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false,
Proxy = new WebProxy("http://localhost:8888", false, new string[]),
UseProxy = true,
;

// snip out some irrelevant setting of authentication cookies

var client = new HttpClient(handler)
BaseAddress = _prefServerBaseUrl,
;

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

return client;



then I send the request by:



var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;


Request goes straight to the server without attempting to hit the proxy. What did I miss?










share|improve this question
























  • Is your service under test on your local machine? Consider proxy bypassing for local addresses.

    – Xaqron
    May 13 '13 at 16:33












  • By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

    – NathanAldenSr
    May 13 '16 at 2:07














50












50








50


8






I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.



Here's how I create the client:



private HttpClient CreateHttpClient(CommandContext ctx, string sid) 
var cookies = new CookieContainer();

var handler = new HttpClientHandler
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false,
Proxy = new WebProxy("http://localhost:8888", false, new string[]),
UseProxy = true,
;

// snip out some irrelevant setting of authentication cookies

var client = new HttpClient(handler)
BaseAddress = _prefServerBaseUrl,
;

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

return client;



then I send the request by:



var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;


Request goes straight to the server without attempting to hit the proxy. What did I miss?










share|improve this question
















I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.



Here's how I create the client:



private HttpClient CreateHttpClient(CommandContext ctx, string sid) 
var cookies = new CookieContainer();

var handler = new HttpClientHandler
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false,
Proxy = new WebProxy("http://localhost:8888", false, new string[]),
UseProxy = true,
;

// snip out some irrelevant setting of authentication cookies

var client = new HttpClient(handler)
BaseAddress = _prefServerBaseUrl,
;

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

return client;



then I send the request by:



var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;


Request goes straight to the server without attempting to hit the proxy. What did I miss?







.net asp.net-web-api .net-4.5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 29 '16 at 14:57









Uwe Keim

27.9k32140220




27.9k32140220










asked May 13 '13 at 16:17









Mike RuhlinMike Ruhlin

2,85011630




2,85011630












  • Is your service under test on your local machine? Consider proxy bypassing for local addresses.

    – Xaqron
    May 13 '13 at 16:33












  • By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

    – NathanAldenSr
    May 13 '16 at 2:07


















  • Is your service under test on your local machine? Consider proxy bypassing for local addresses.

    – Xaqron
    May 13 '13 at 16:33












  • By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

    – NathanAldenSr
    May 13 '16 at 2:07

















Is your service under test on your local machine? Consider proxy bypassing for local addresses.

– Xaqron
May 13 '13 at 16:33






Is your service under test on your local machine? Consider proxy bypassing for local addresses.

– Xaqron
May 13 '13 at 16:33














By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

– NathanAldenSr
May 13 '16 at 2:07






By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google.

– NathanAldenSr
May 13 '16 at 2:07













3 Answers
3






active

oldest

votes


















49














This code worked for me:



var httpClientHandler = new HttpClientHandler

Proxy = new WebProxy("http://localhost:8888", false),
UseProxy = true



Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?






share|improve this answer























  • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

    – Casey Burns
    Nov 15 '14 at 10:18






  • 1





    You just saved my day ^_^

    – Wahid Bitar
    Mar 17 '17 at 8:04


















7














Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.



I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.






share|improve this answer


















  • 7





    You can also use http://localhost.:8888 Notice the magic dot!

    – Darrel Miller
    May 14 '13 at 0:08






  • 2





    I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

    – Michael
    May 16 '13 at 16:01











  • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

    – felickz
    Aug 25 '14 at 20:51



















2














Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16526689%2fusing-a-proxy-with-net-4-5-httpclient%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    49














    This code worked for me:



    var httpClientHandler = new HttpClientHandler

    Proxy = new WebProxy("http://localhost:8888", false),
    UseProxy = true



    Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?






    share|improve this answer























    • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

      – Casey Burns
      Nov 15 '14 at 10:18






    • 1





      You just saved my day ^_^

      – Wahid Bitar
      Mar 17 '17 at 8:04















    49














    This code worked for me:



    var httpClientHandler = new HttpClientHandler

    Proxy = new WebProxy("http://localhost:8888", false),
    UseProxy = true



    Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?






    share|improve this answer























    • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

      – Casey Burns
      Nov 15 '14 at 10:18






    • 1





      You just saved my day ^_^

      – Wahid Bitar
      Mar 17 '17 at 8:04













    49












    49








    49







    This code worked for me:



    var httpClientHandler = new HttpClientHandler

    Proxy = new WebProxy("http://localhost:8888", false),
    UseProxy = true



    Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?






    share|improve this answer













    This code worked for me:



    var httpClientHandler = new HttpClientHandler

    Proxy = new WebProxy("http://localhost:8888", false),
    UseProxy = true



    Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 10 '14 at 12:57









    NathanAldenSrNathanAldenSr

    6,39733045




    6,39733045












    • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

      – Casey Burns
      Nov 15 '14 at 10:18






    • 1





      You just saved my day ^_^

      – Wahid Bitar
      Mar 17 '17 at 8:04

















    • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

      – Casey Burns
      Nov 15 '14 at 10:18






    • 1





      You just saved my day ^_^

      – Wahid Bitar
      Mar 17 '17 at 8:04
















    Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

    – Casey Burns
    Nov 15 '14 at 10:18





    Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost.

    – Casey Burns
    Nov 15 '14 at 10:18




    1




    1





    You just saved my day ^_^

    – Wahid Bitar
    Mar 17 '17 at 8:04





    You just saved my day ^_^

    – Wahid Bitar
    Mar 17 '17 at 8:04













    7














    Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.



    I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.






    share|improve this answer


















    • 7





      You can also use http://localhost.:8888 Notice the magic dot!

      – Darrel Miller
      May 14 '13 at 0:08






    • 2





      I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

      – Michael
      May 16 '13 at 16:01











    • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

      – felickz
      Aug 25 '14 at 20:51
















    7














    Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.



    I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.






    share|improve this answer


















    • 7





      You can also use http://localhost.:8888 Notice the magic dot!

      – Darrel Miller
      May 14 '13 at 0:08






    • 2





      I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

      – Michael
      May 16 '13 at 16:01











    • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

      – felickz
      Aug 25 '14 at 20:51














    7












    7








    7







    Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.



    I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.






    share|improve this answer













    Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.



    I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 13 '13 at 16:34









    Mike RuhlinMike Ruhlin

    2,85011630




    2,85011630







    • 7





      You can also use http://localhost.:8888 Notice the magic dot!

      – Darrel Miller
      May 14 '13 at 0:08






    • 2





      I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

      – Michael
      May 16 '13 at 16:01











    • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

      – felickz
      Aug 25 '14 at 20:51













    • 7





      You can also use http://localhost.:8888 Notice the magic dot!

      – Darrel Miller
      May 14 '13 at 0:08






    • 2





      I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

      – Michael
      May 16 '13 at 16:01











    • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

      – felickz
      Aug 25 '14 at 20:51








    7




    7





    You can also use http://localhost.:8888 Notice the magic dot!

    – Darrel Miller
    May 14 '13 at 0:08





    You can also use http://localhost.:8888 Notice the magic dot!

    – Darrel Miller
    May 14 '13 at 0:08




    2




    2





    I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

    – Michael
    May 16 '13 at 16:01





    I'm getting a Bad Request - Invalid Host Name when I put the . after localhost

    – Michael
    May 16 '13 at 16:01













    @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

    – felickz
    Aug 25 '14 at 20:51






    @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>

    – felickz
    Aug 25 '14 at 20:51












    2














    Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.






    share|improve this answer



























      2














      Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.






      share|improve this answer

























        2












        2








        2







        Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.






        share|improve this answer













        Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 13 '13 at 16:30









        BadriBadri

        17k35256




        17k35256



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16526689%2fusing-a-proxy-with-net-4-5-httpclient%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현