我正在尝试使用授权(NTLM / BASIC)发出 GET/Post 请求。代码适用于所有 GET 请求,但对于任何 POST 请求都会获得 401 未授权。
我的代码:
NetworkCredential credential = new NetworkCredential(user, password);
var myCache = new CredentialCache
{
{ uri, auth, credential } // auth = "BASIC" or "NTLM"
};
var handler = new HttpClientHandler
{
AllowAutoRedirect = true,
PreAuthenticate = true,
Credentials = myCache
};
HttpClient httpClient = new HttpClient(handler);
...
StringContent content = new StringContent(bodyText);
httpClient.PostAsync(uri, content).Result; // returns 401