Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to call web APi from c# code behind. This is my function added in my code

private async void GetToken()
      {


          using (HttpClient client = new HttpClient())
          {
              var request_json = "your json string";

              HttpContent content = new StringContent(request_json, Encoding.UTF8, "application/json");



              client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Integration", "bhtrdimh=");
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

              var httpResponseMessage = await client.PostAsync("https://xxx.xx.com/xx/xx/token", content);
             var r = await httpResponseMessage.Content.ReadAsStringAsync();
           }


      }

After use this code i getting error

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1061	'Task<HttpResponseMessage>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'Task<HttpResponseMessage>' could be found (are you missing a using directive or an assembly reference?)	CheckboxWeb	C:\Project\RogerDev1\CBE\survey-platform\CheckboxWeb\Forms\Surveys\Invitations\NBVUserlink.aspx.cs	194	Active

I don't understand where in my Problem.
Can any one do this type of work then please guide me with some sample code.
Thank you in advance

What I have tried:

search in google but no suitable answer found that help me
Posted
Updated 18-Jul-18 22:35pm
Comments
Vincent Maverick Durano 16-Jul-18 0:14am    
What version of .NET are you using?
Member 13907608 16-Jul-18 1:57am    
thank you for reply
now i change my code
like below

using (var client = new System.Net.Http.HttpClient())
{
client.BaseAddress = new Uri("https://xxx.xxx.com");
client.DefaultRequestHeaders.Accept.Clear();
HttpContent content = new StringContent(request_json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Integration", "bmcEpOm0=");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = client.PostAsync("external/xxx/token", content).Result;
if (response.IsSuccessStatusCode)
{
string responseString = response.Content.ReadAsStringAsync().Result;
Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(responseString);

}
}
now it's return bad request
Vincent Maverick Durano 16-Jul-18 6:39am    
Bad Request indicates that the server could not understand the request due to invalid syntax or the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules. Please double check your endpoint URL.
Member 13907608 16-Jul-18 10:33am    
Thanks problem is solve. There was my fault.
Actually the api accept get and i send post . That's why getting error
ZurdoDev 16-Jul-18 11:42am    
Please post as solution so that this no longer shows up as Unanswered.

in my code i use
var response = client.PostAsync("external/xxx/token", content).Result;

but when i change to
var response = client.GetAsync("external/xxx/token", content).Result;


it's work
 
Share this answer
 
string apiUrl="http://localhost:36727/api/yourapiclass";
using (var client = new HttpClient())
              {
                  var json = JsonConvert.SerializeObject(YourModel);
                  var stringContent = new StringContent(json, Encoding.UTF8,
                              "application/json");
                  model = client
                                     .PostAsync(apiUrl + "/YourActionName", stringContent)
                                     .Result
                                     .Content.ReadAsAsync<ResponseModel>().Result;
              }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900