Web api调用C#代码后面 [英] Web api call in C# code behind

查看:94
本文介绍了Web api调用C#代码后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从c#代码后面调用web APi。这是我在我的代码中添加的功能



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


}



使用此代码后我收到错误



严重级代码描述项目文件行抑制状态
错误CS1061'Task< HttpResponseMessage>'不包含'GetAwaiter'的定义,也没有扩展方法' GetAwaiter接受型的第一参数‘任务< HttpResponseMessage>’可以找到(是否缺少using指令或程序集引用?)CheckboxWeb C:\Project\RogerDev1\CBE\survey-platform\CheckboxWeb \Forms\Surveys\Invitations\NBVUserlink.aspx.cs 194 Active



我不明白我的问题在哪里。

任何人都可以做这类工作然后请给我一些示例代码。

提前谢谢



什么我试过了:



搜索谷歌但找不到合适的答案帮我

解决在我的代码中我使用了
$ block $>

 var response = client.PostAsync(external / xxx / token,content).Result; 



但当我改为

 var response = client.GetAsync(external / xxx / token,content).Result ; 





它的工作


string apiUrl =http:// localhost:36727 / api / yourapiclass;

使用(VAR客户=新的HttpClient())
{
变种JSON = JsonConvert.SerializeObject(YourModel);
var stringContent = new StringContent(json,Encoding.UTF8,
application / json);
型=客户端
.PostAsync(apiUrl + / YourActionName 的StringContent)
。结果
.Content.ReadAsAsync< ResponseModel>()结果;
}


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

解决方案

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


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;
              }


这篇关于Web api调用C#代码后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆