gpt4 book ai didi

c# - 调用 HTTPClient.PostAsync 时设置授权/内容类型 header

转载 作者:太空狗 更新时间:2023-10-29 22:07:07 25 4
gpt4 key购买 nike

当使用简单的 HTTPClient 时,我在哪里可以设置 REST 服务调用的 header ?

我愿意:

HttpClient client = new HttpClient();
var values = new Dictionary<string, string>
{
{"id", "111"},
{"amount", "22"}
};
var content = new FormUrlEncodedContent(values);
var uri = new Uri(@"https://some.ns.restlet.uri");

var response = await client.PostAsync(uri, content);
var responseString = await response.Content.ReadAsStringAsync();

更新

我要添加的标题:

{
"Authorization": "NLAuth nlauth_account=5731597_SB1, nlauth_email=xxx@xx.com, nlauth_signature=Pswd1234567, nlauth_role=3",
"Content-Type": "application/json"
}

我应该执行以下操作吗?

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "NLAuth nlauth_account=5731597_SB1, nlauth_email=xxx@xx.com, nlauth_signature=Pswd1234567, nlauth_role=3","Content-Type":"application/json");

最佳答案

添加header的方法如下:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");

或者如果你想要一些自定义标题:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("HEADERNAME", "HEADERVALUE");

这个答案已经有 SO 回复,见下文:

更新

似乎您要添加两个 header ;授权和内容类型。

string authValue = "NLAuth nlauth_account=5731597_SB1,nlauth_email=xxx@xx.com, nlauth_signature=Pswd1234567, nlauth_role=3";
string contentTypeValue = "application/json";

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authValue);
client.DefaultRequestHeaders.Add("Content-Type", contentTypeValue);

关于c# - 调用 HTTPClient.PostAsync 时设置授权/内容类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com