C# HttpClient获取呼叫返回状态代码401未在C中授权#
我试图通过添加一些头来通过C#调用HttpClient。通过抛出预期的空结果(状态代码为200),请求工作正常。但在C#代码中也不适用。我的代码如下:C# HttpClient获取呼叫返回状态代码401未在C中授权#,c#,winapi,asp.net-web-api,httpclient,C#,Winapi,Asp.net Web Api,Httpclient,我试图通过添加一些头来通过C#调用HttpClient。通过抛出预期的空结果(状态代码为200),请求工作正常。但在C#代码中也不适用。我的代码如下: public static Response TestAbcAPICall() { Response response = new Response(); try { var endpoint = "https:/a
public static Response TestAbcAPICall()
{
Response response = new Response();
try
{
var endpoint = "https:/abc.com/path/?script=435&deploy=1&type=vendor&id=9797";
using (var client = new System.Net.Http.HttpClient())
{
client.BaseAddress = new Uri(endpoint);
//way - 1 Adding Authorization header
var authenticationHeaderValue = new AuthenticationHeaderValue("Authorization", "NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
// Adding contennt-type header
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var getTask = client.GetAsync(endpoint);
getTask.Wait();
var result = getTask.Result;
if (result.IsSuccessStatusCode)
{
var RESULTS = result.Content.ReadAsStringAsync().Result;
//response = JsonConvert.DeserializeObject<Response>(RESULTS);
if (response.flag)
{
response.flag = true;
}
else
{
response.flag = false;
}
}
}
}
catch (Exception ex)
{
response.message = ex.Message;
response.flag = false;
}
return response;
}
public静态响应TestAbcAPICall()
{
响应=新响应();
尝试
{
var endpoint=“https:/abc.com/path/?script=435&deploy=1&type=vendor&id=9797”;
使用(var client=new System.Net.Http.HttpClient())
{
client.BaseAddress=新Uri(端点);
//方式-1添加授权标头
var authenticationHeaderValue=新的authenticationHeaderValue(“授权”,“NLAuth NLAuth_帐户=1249484_SKB1,NLAuth_电子邮件=restletuser1@org.in,nlauth_签名=呼吸123!,nlauth_角色=4444”);
client.DefaultRequestHeaders.Authorization=authenticationHeaderValue;
//添加contennt类型头
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
var getTask=client.GetAsync(端点);
getTask.Wait();
var result=getTask.result;
if(结果。IsSuccessStatusCode)
{
var RESULTS=result.Content.ReadAsStringAsync().result;
//response=JsonConvert.DeserializeObject(结果);
if(response.flag)
{
response.flag=true;
}
其他的
{
response.flag=false;
}
}
}
}
捕获(例外情况除外)
{
response.message=ex.message;
response.flag=false;
}
返回响应;
}
使用200状态图像参考ARC工作:发送请求时,您的
AuthenticationHeaderValue
auth标头将如下所示:
Authorization: Authorization NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444
正如您所看到的,重复的授权
会破坏它
而是使用:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
另一方面,您实际上只需要创建一个
HttpClient
实例,并在应用程序的整个生命周期内重复使用它。发送请求时,您的AuthenticationHeaderValue
auth头将如下所示:
Authorization: Authorization NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444
正如您所看到的,重复的授权
会破坏它
而是使用:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
顺便说一句,您实际上只需要创建一个
HttpClient
实例,并在应用程序的整个生命周期中重用它。您的身份验证应该如下所示
var authenticationHeaderValue = new AuthenticationHeaderValue("NLAuth", "nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
或
已经有关于stackoverflow的帖子涉及此问题您的身份验证应该如下所示
var authenticationHeaderValue = new AuthenticationHeaderValue("NLAuth", "nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
或
stackoverflow上已经有关于此问题的帖子client.BaseAddress不需要设置,您已经有完整的地址。@Alen.Toma是的,当然谢谢,评论道。仍然面对相同的..工作代码使用HTTPS(安全)。你对c有什么反应吗?如果没有收到响应,则TLS身份验证失败,这发生在发送请求之前。通常的解决方案是指定TLS版本:ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;我想他是gettings响应,试着看看响应是什么,例如
result.StatusCode==HttpStatusCode.Unauthorized
,这也是Get请求,所以你不需要设置application/json
,你可以将它作为字符串读取是的,在“getTask”中,我将“result.IsSuccessStatusCode”设置为“false”不需要设置client.BaseAddress,您已经有完整的地址。@Alen.Toma是的,当然谢谢,评论道。仍然面对相同的..工作代码使用HTTPS(安全)。你对c有什么反应吗?如果没有收到响应,则TLS身份验证失败,这发生在发送请求之前。通常的解决方案是指定TLS版本:ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;我想他是gettings响应,试着看看响应是什么,例如result.StatusCode==HttpStatusCode.Unauthorized
,这也是Get请求,所以你不需要设置application/json
,你可以将它作为字符串读取是的,在“getTask”中,我将“result.IsSuccessStatusCode”设置为“false”当我实现你的代码时,它抛出了400个状态码,请求错误。运行Fiddler并捕获原始请求,与邮递员帖子相比,情况会有所不同。正如我通过“客户端”对象中的快速观察观察所观察到的,它取空表示“授权”。但是,你所说的是正确的,关于附加“授权”当我使用fiddler进行测试时,我观察到“Accept:application/json”,但我们需要将其作为“content-type”进行传递。当我实现您的代码时,它抛出400个状态码,请求错误。运行fiddler并捕获原始请求,与邮递员帖子相比,情况会有所不同。正如我在“客户端”中通过快速观察观察所观察到的对象的“授权”为空。但是,您所说的是正确的,关于将“授权”附加到值。当我使用fiddler进行测试时,我观察到“接受:应用程序/json”,但我们需要将其作为“内容类型”传递。Toma是第一个添加标题但给出401个未经授权的响应的对象。第二个甚至不接受作为头,而是抛出类似异常的格式是无效的。Toma,第一个添加头,但给出401个未经授权的响应。第二个甚至不接受作为头,而抛出类似异常的格式是无效的。