C# HttpClient在API中发布数据时遇到的问题
我的代码在发布数据时显示以下错误:“错误:401未授权” 我的班级:C# HttpClient在API中发布数据时遇到的问题,c#,.net,api,dotnet-httpclient,C#,.net,Api,Dotnet Httpclient,我的代码在发布数据时显示以下错误:“错误:401未授权” 我的班级: public class APICommands : IDisposable { public APICommands() { this.HttpClientHandler = new HttpClientHandler(); // Set authentication. this.HttpClientHandler.UseDefaultCredentials
public class APICommands : IDisposable
{
public APICommands()
{
this.HttpClientHandler = new HttpClientHandler();
// Set authentication.
this.HttpClientHandler.UseDefaultCredentials = false;
this.HttpClientHandler.Credentials = new NetworkCredential("username@myemail.com", "mypassword");
this.HttpClient = new HttpClient(this.HttpClientHandler);
this.HttpClient.BaseAddress = new Uri("https://api.myhost.com");
this.HttpClient.DefaultRequestHeaders.Accept.Clear();
this.HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
private HttpClient HttpClient { get; set; }
private HttpClientHandler HttpClientHandler { get; set; }
public async Task<JsonResultBoleto> CreateClient(string name, string age)
{
ServicePointManager.Expect100Continue = false;
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("name", name));
postData.Add(new KeyValuePair<string, string>("age", age));
HttpContent content = new FormUrlEncodedContent(postData);
// When I call this method "PostAsync", the error message is displayed.
HttpResponseMessage response = await this.HttpClient.PostAsync("https://api.myhost.com/client/", content);
if (response.IsSuccessStatusCode)
{
// Do something.
}
return null;
}
}
public类APICommands:IDisposable
{
公共APICommands()
{
this.HttpClientHandler=新的HttpClientHandler();
//设置身份验证。
this.HttpClientHandler.UseDefaultCredentials=false;
this.HttpClientHandler.Credentials=新网络凭据(“username@myemail.com“,“我的密码”);
this.HttpClient=新的HttpClient(this.HttpClientHandler);
this.HttpClient.BaseAddress=新Uri(“https://api.myhost.com");
this.HttpClient.DefaultRequestHeaders.Accept.Clear();
this.HttpClient.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
}
私有HttpClient HttpClient{get;set;}
私有HttpClientHandler HttpClientHandler{get;set;}
公共异步任务CreateClient(字符串名称、字符串期限)
{
ServicePointManager.Expect100Continue=false;
var postData=新列表();
添加(新的KeyValuePair(“name”,name));
添加(新的KeyValuePair(“年龄”,年龄));
HttpContent=新的FormUrlEncodedContent(postData);
//当我调用此方法“PostAsync”时,将显示错误消息。
HttpResponseMessage response=等待此消息。HttpClient.PostAsync(“https://api.myhost.com/client/“,内容);
if(响应。IsSuccessStatusCode)
{
//做点什么。
}
返回null;
}
}
添加此代码时开始出现错误:ServicePointManager.Expect100Continue=false代码>。我添加此代码是为了解决另一个错误:“417-期望失败”:(
你要去干什么
感谢…身份验证机制似乎没有响应401-未经授权的
响应。您可以将预身份验证设置添加到HttpClientHandler,以在初始请求期间强制发送凭据,而不是等待授权质询
...
// Set authentication.
this.HttpClientHandler.UseDefaultCredentials = false;
this.HttpClientHandler.Credentials = new NetworkCredential("username@myemail.com", "mypassword");
this.HttpClientHandler.PreAuthenticate = true;
我认为不需要ServicePointManage.Expect100Continue
。我假设凭证不起作用
为什么不通过授权头试试呢:
string authInfo = "username@myemail.com:mypassword";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);
www authenticate头在401响应上说了什么?我收到这个头:Digest realm=“Area restrita.”,qop=“auth”,nonce=“532c97204bcad”,opaque=“02c932a49c1224ef22b6726872e674e5”哇。祝你好运。我从未尝试过使用摘要身份验证。请尝试使用CredentialCache,而不仅仅是一个简单的NetworkCredential。尽管我对CredentialCache处理领域的方式表示怀疑。您好,感谢您的回复,但它不起作用,相同的错误!:(