C# 调用HTTPClient.PostAsync时设置授权/内容类型标题
在使用简单HTTPClient时,在哪里可以设置REST服务调用的头 我有:C# 调用HTTPClient.PostAsync时设置授权/内容类型标题,c#,netsuite,C#,Netsuite,在使用简单HTTPClient时,在哪里可以设置REST服务调用的头 我有: HttpClient client = new HttpClient(); var values = new Dictionary<string, string> { {"id", "111"}, {"amount", "22"} }; var content = new FormUrlEncodedContent(values); var uri = new Uri(@"https://s
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();
我应该做以下事情吗
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");
添加标题的方法如下所示:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
或者,如果您需要一些自定义标题:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("HEADERNAME", "HEADERVALUE");
这个答案已经有了答案,请参见下文:
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);
我知道这是前一段时间提出的,但胡安的解决方案对我不起作用 (另外,我很确定这个问题是重复的。) 最终有效的方法是将HttpClient与和一起使用 还请注意,这是使用Newtonsoft的
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using Newtonsoft.Json;
namespace NetsuiteConnector
{
class Netsuite
{
public void RunHttpTest()
{
Task t = new Task(TryConnect);
t.Start();
Console.WriteLine("Connecting to NS...");
Console.ReadLine();
}
private static async void TryConnect()
{
// dummy payload
String jsonString = JsonConvert.SerializeObject(
new NewObj() {
Name = "aname",
Email = "someone@somewhere.com"
}
);
string auth = "NLAuth nlauth_account=123456,nlauth_email=youremail@somewhere.com,nlauth_signature=yourpassword,nlauth_role=3";
string url = "https://somerestleturl";
var uri = new Uri(@url);
HttpClient c = new HttpClient();
c.BaseAddress = uri;
c.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", auth);
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url);
req.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
HttpResponseMessage httpResponseMessage = await c.SendAsync(req);
httpResponseMessage.EnsureSuccessStatusCode();
HttpContent httpContent = httpResponseMessage.Content;
string responseString = await httpContent.ReadAsStringAsync();
Console.WriteLine(responseString);
}
}
class NewObj
{
public string Name { get; set; }
public string Email { get; set; }
}
}
如果您使用的是HttpClientFactory,那么其他答案就不起作用,为什么应该这样做。对于HttpClientFactory,HttpMessages从池中重用,因此应该为每个请求中使用的头保留设置默认头 如果您只想添加内容类型标题,可以使用备用的
PostAsJsonAsync
或PostAsXmlAsync
var response = await _httpClient.PostAsJsonAsync("account/update", model);
不幸的是,我没有比这更好的添加授权头的解决方案
_httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.Authorization.ToString(), $"Bearer {bearer}");
您正在寻找这个
client.DefaultRequestHeaders.Add(“Accept”,“application/json”)代码>您希望添加哪些标题?有不同的方法添加不同的标题,例如Accept headerHttpClient.DefaultRequestHeaders.Accept
可能与See重复。我已用需要添加的标题更新了问题正文。我想我应该使用授权头?在这里查看Alaa Masoud的答案:了解更多详细信息。
_httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.Authorization.ToString(), $"Bearer {bearer}");