Hi all I am working on a project. how to solve the (407) Proxy Authentication Required error and get through proxy.
The project need to POST some data to server via proxy. The proxy enforce credentials so that the application are required to aware NTLM/Kerberos credentials in Windows Domain User environment.
Applications like Chrome will automatically get the credentials for proxy, and get it through.
My project do well if the proxy server does not enforce credentials, but does not work when enforced.
The code I write here is:
using Flurl.Http;
using Flurl.Http.Configuration;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace HttpPostProxyDemo
{
public class WindowsAuthClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new HttpClientHandler { UseDefaultCredentials = true };
}
}
class Program
{
static string url = "https://httpbin.org/status/200";
static async Task Main(string[] args)
{
FlurlHttp.ConfigureClient("https://httpbin.org", cli =>
cli.Settings.HttpClientFactory = new WindowsAuthClientFactory());
var result = await url.PostJsonAsync("");
Console.WriteLine(result.StatusCode.ToString());
Thread.Sleep(60000);
}
}
}
If no proxy or proxy does not enforce credentials, it will print OK, because the app sends 200 to the API, and get OK back.
But under domain user environment, and enforce proxy credentials, it gives me following exceptions:
Unhandled Exception: Flurl.Http.FlurlHttpException: Call failed. An error occurred while sending the request. POST https://httpbin.org/status/200 ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext()
--- End of inner exception stack trace ---
at Flurl.Http.FlurlRequest.<HandleExceptionAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at HttpPostProxyDemo.Program.<Main>d__1.MoveNext() in Z:\HttpPostProxyDemo\HttpPostProxyDemo\Program.cs:line 31
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at HttpPostProxyDemo.Program.<Main>(String[] args)