2017-06-01 37 views
0

使用简单的HTTPClient时,我可以在哪里设置标题为REST服务调用?呼叫HTTPClient.PostAsync时设置标题

我做的:

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(); 

UPD

页眉我不会补充的是:

{"Authorization":"NLAuth nlauth_account=5731597_SB1, [email protected], nlauth_signature=Pswd1234567, nlauth_role=3","Content-Type":"application/json"} 

应该怎么办遵循:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "NLAuth nlauth_account=5731597_SB1, [email protected], nlauth_signature=Pswd1234567, nlauth_role=3","Content-Type":"application/json"); 
+0

您是否在寻找这个'client.DefaultRequestHeaders.Add( “接受”, “应用/ JSON”);' – Sakis

+0

什么头你正在寻找购买?有不同的方法可以添加不同的头文件,例如Accept Header'HttpClient.DefaultRequestHeaders.Accept' – Gururaj

+0

[使用httpClient.GetAsync时添加头文件]的可能重复(https://stackoverflow.com/questions/29801195/adding-headers- when-using-httpclient-getasync) –

回答

1

添加标题的方式如下:

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

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

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

这个答案有如此的反应已经,见下图:

UPDATE

似乎你添加两个标题;授权和内容类型。

string authValue = "NLAuth nlauth_account=5731597_SB1,[email protected], nlauth_signature=Pswd1234567, nlauth_role=3"; 
string contentTypeValue = "application/json"; 

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

我已经更新了需要添加标头的问题主体。我想我应该使用授权标题? – vico

+0

查看来自Alaa Masoud的答案:https://stackoverflow.com/questions/19039450/adding-authorization-to-the-headers了解更多详情。 –

0

我知道这是前一段时间问的,但胡安的解决方案并不适用于我。

(而且,很肯定这个问题是重复here。)

终于工作的方法是使用HttpClient的与HttpRequestMessageHttpResponseMessage

另请注意,这是使用Newtonsoft的Json.NET

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 = "[email protected]" 
        } 
       ); 

       string auth = "NLAuth nlauth_account=123456,[email protected],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; } 
     } 
    }