簡體   English   中英

在Windows域用戶環境中通過代理進行POST時,C#中的Fluent出現(407)代理身份驗證必需錯誤

[英]Flurl in C# get (407) Proxy Authentication Required error when POST via proxy in Windows Domain User environment

大家好,我正在做一個項目。 如何解決(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);
    }
  }
}

如果沒有代理或沒有代理不強制執行憑據,則它將打印“確定”,因為該應用會將200發送到API,並取回“確定”。

但是在域用戶環境下,並強制執行代理憑據,它給了我以下例外:

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)

我認為您可以執行此操作,向用戶詢問他的Windows憑據,並使用其用戶名和密碼來提供UseDefaultCredentials。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM