C# 中的 flurl get (407) 在 Windows 域用户环境中通过代理开机自检时出错,需要代理身份验证错误



大家好,我正在做一个项目。 如何解决(407(需要代理身份验证错误并通过代理。

项目需要通过代理将一些数据发布到服务器。代理强制实施凭据,以便应用程序需要识别 Windows 域用户环境中的 NTLM/Kerberos 凭据。

像Chrome这样的应用程序将自动获取代理凭据,并通过它。

如果代理服务器不强制实施凭据,但在强制执行时不起作用,我的项目会做得很好。

我在这里编写的代码是:

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);
}
}
}

如果没有代理或代理不强制实施凭据,它将打印 OK,因为应用将 200 发送到 API,并返回 OK。

但是在域用户环境中,并强制执行代理凭据,它给了我以下例外:

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:HttpPostProxyDemoHttpPostProxyDemoProgram.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)

我认为你可以这样做,询问用户他的Windows凭据,并使用他们的用户名和密码提供UseDefaultCredentials。

相关内容

最新更新