2017-10-14 3 views
3

このカールスクリプトに相当するものをC#で記述したいと思います。C#でAPIキーを使用してHttpClientクラスのPostAsyncメソッドを使用する

  async static void PostRequest() 
     { 
      string url="example.com/login" 
      var formData = new List<KeyValuePair<string, string>>(); 
      formData.Add(new KeyValuePair<string, string>("username", "[email protected]")); 
      formData.Add(new KeyValuePair<string, string>("password", "mypassword")); 
      HttpContent q = new FormUrlEncodedContent(formData); 
      // where do I put my api key? 
      using (HttpClient client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header 
       client.BaseAddress = new Uri(url); 
       using (HttpResponseMessage response = await client.PostAsync(url, q)) 
       { 
        using (HttpContent content =response.Content) 
        { 
         string mycontent = await content.ReadAsStringAsync(); 


        } 
       } 
      } 

     } 

私の質問はどのように私は私の要求にAPIキーが含まれますされています。私が書いてきた

curl -X POST \ 
https://example.com/login \ 
-H 'api-key: 11111111' \ 
-H 'cache-control: no-cache' \ 
-H 'content-type: application/json' \ 
-d '{ 
"username": "[email protected], 
"password": "mypassword" 
}' 

対応するC#のコードは以下の通りです:私のカールスクリプトは、次のでしょうか?

答えて

2

あなたが呼び出しているApiでは、キーがヘッダーにあるかのように見えます。
ので使用:

client.DefaultRequestHeaders.Add("api-key", "11111111"); 
+0

を、それはヘッダ – user3841581

+1

に行く必要が期待どおりに動作します – user3841581

関連する問題