none
HttpClientHandler proxy is not called when POST request to the server. RRS feed

  • Question

  • I am using HttpClient object to POST a request message to the server. The HttpClientHandler defined to use a Proxy. The proxy is ignored with the POST message and never used when posting to the server. No errors or exceptions reported. 

    Code spinets: 

    var handler = new HttpClientHandler
                        {
                            UseDefaultCredentials = true,
                            UseProxy = true,
                            ClientCertificateOptions = ClientCertificateOption.Automatic,
                            Proxy = myProxy,
                            AllowAutoRedirect = false
                        };

    var httpContent = new StringContent(requestString, Encoding.ASCII, "application/xml");
                    var result = await httpClient.PostAsync(url, httpContent);
                    resultAsString = await result.Content.ReadAsStringAsync(); 

    Do you have idea what could be went wrong? 

    Regards

    Wednesday, June 12, 2019 9:52 PM

Answers

  • This is an ASP.NET/.NET Core issue then. Setting a handler via the standard HttpClient works fine.

    Please post questions related to ASP.NET in the ASP.NET forums. For issues with HttpClient in .NET Core you'll want to post in GitHub. Specifically you'll see that there are already some issues written up about proxies with HttpClient.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by eddy1234_676 Friday, June 14, 2019 7:31 PM
    Thursday, June 13, 2019 2:31 PM
    Moderator
  • The code to setup the HttpClient handler is from the ConfigureServices in the Startup class: 

    The code is .net Core 2.x

    Huh? The services in .NET Core are designed for dependency injection of an object. It's an IoC container.

     https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

    I think you may have some kind of mis-understanding about .NET Core service used in ASP.NET Core.

    • Marked as answer by eddy1234_676 Friday, June 14, 2019 7:31 PM
    Thursday, June 13, 2019 2:55 PM

All replies

  • Hi eddy,

    Thank you for posting here.

    Based on my test, the code could call proxy successfully. I think you could try to set break point to check it.

    If you change UseProxy to false, it will throw an exception.

    I am not sure what your current question is, you could see the following code that I write.

     string url = "http://www.contoso.com";
                WebRequest request = WebRequest.Create("http://www.contoso.com");
                // This application doesn't want the proxy to be used so it sets 
                // the global proxy to an empty proxy.
                IWebProxy myProxy = GlobalProxySelection.GetEmptyWebProxy();
                var handler = new HttpClientHandler
                {
                    UseDefaultCredentials = true,
                    UseProxy = true,
                    ClientCertificateOptions = ClientCertificateOption.Automatic,
                    Proxy = myProxy,
                    AllowAutoRedirect = false
                };
                HttpClient httpClient = new HttpClient(handler);
                string requestString = "test1";
                var httpContent = new StringContent(requestString, Encoding.ASCII, "application/xml");
                var result = await httpClient.PostAsync(url, httpContent);
                var text= await result.Content.ReadAsStringAsync();

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 13, 2019 6:42 AM
    Moderator
  • Handlers are configured when you create the HttpClient. Where is that code?

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, June 13, 2019 1:53 PM
    Moderator
  • HI Michael, 

    The code to setup the HttpClient handler is from the ConfigureServices in the Startup class: 

    The code is .net Core 2.x


    
    Thursday, June 13, 2019 2:13 PM
  • Basically, my code does not call the proxy. One test i did is to shutdown the proxy and still the service does work successfully. 
    Thursday, June 13, 2019 2:24 PM
  • This is an ASP.NET/.NET Core issue then. Setting a handler via the standard HttpClient works fine.

    Please post questions related to ASP.NET in the ASP.NET forums. For issues with HttpClient in .NET Core you'll want to post in GitHub. Specifically you'll see that there are already some issues written up about proxies with HttpClient.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by eddy1234_676 Friday, June 14, 2019 7:31 PM
    Thursday, June 13, 2019 2:31 PM
    Moderator
  • The code to setup the HttpClient handler is from the ConfigureServices in the Startup class: 

    The code is .net Core 2.x

    Huh? The services in .NET Core are designed for dependency injection of an object. It's an IoC container.

     https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

    I think you may have some kind of mis-understanding about .NET Core service used in ASP.NET Core.

    • Marked as answer by eddy1234_676 Friday, June 14, 2019 7:31 PM
    Thursday, June 13, 2019 2:55 PM