简体   繁体   English

ASP.NET Web Forms:对外发起API请求并在页面显示响应

[英]ASP.NET Web Forms: Make an external API Request and display response on page

I've found a lot of posts for ASP.NET Web API but nothing at all for classic Web Forms. I want to call an external API and display its result on a web page text field.我发现了很多关于 ASP.NET Web API 的帖子,但没有找到经典 Web Forms 的帖子。我想调用外部 API 并将其结果显示在 883658850 文本字段上。 But even though the API call is successful, the response is not received (or at least not shown on the page.但是即使 API 调用成功,也没有收到响应(或者至少没有在页面上显示。

C# code behind: C# 后面的代码:

protected void btnOptimize_Click(object sender, EventArgs e) {
  Optimize();
}

public async Task Optimize() {
  string URI = "https://EXTERNAL_API_BASE_URL/POST";
  string auth = "Bearer XXXX";

  var client = new HttpClient();

  MyData data = new MyData();
  data.text = "Hello world";

  string requestBody = JsonConvert.SerializeObject(data);

  var stringContent = new StringContent(requestBody);

  client.DefaultRequestHeaders.Add("Content-Type", "application/json");
  client.DefaultRequestHeaders.Add("Authorization", auth);

  var response = await client.PostAsync(URI, stringContent);

  //display on page's text field
  txtResponse.Value = response.Content.ToString();
}

ASP.NET Page: ASP.NET 页:

<body>
    <form id="form1" runat="server">
        <div>
            <textarea runat="server" id="txtInput"></textarea>
            <asp:Button ID="btnOptimize" runat="server" OnClick="btnOptimize_Click" Text="Generate" />
            <textarea runat="server" id="txtResponse"></textarea>
        </div>
    </form>
</body>

What should I do differently?我应该怎么做? Should I add an UpdatePanel to my ASP.NET page?我应该将 UpdatePanel 添加到我的 ASP.NET 页面吗?

Read the HttpClient's response with ReadAsStringAsync()使用ReadAsStringAsync()读取 HttpClient 的响应

string responseBody = await response.Content.ReadAsStringAsync();

Please see HttpClient Class请参阅HttpClient Class

Since the Optimize method is async, try changing the code like this.由于 Optimize 方法是异步的,请尝试像这样更改代码。

//First change the method so it's async as well.
protected async Task btnOptimize_Click(object sender, EventArgs e) {
  //Then call the Optimize() method using the await keyword.
  await Optimize();
}

public async Task Optimize() {
  string URI = "https://EXTERNAL_API_BASE_URL/POST";
  string auth = "Bearer XXXX";

  var client = new HttpClient();

  MyData data = new MyData();
  data.text = "Hello world";

  string requestBody = JsonConvert.SerializeObject(data);

  var stringContent = new StringContent(requestBody);

  client.DefaultRequestHeaders.Add("Content-Type", "application/json");
  client.DefaultRequestHeaders.Add("Authorization", auth);

  var response = await client.PostAsync(URI, stringContent);

  //display on the page's text field
  //You should get value using the await keyword.
  //response.content also has a method called ReadAsStringAsync(),
  //it should be visible if you are using intellisense in VisualStudio
  txtResponse.Value = await response.Content.ReadAsStringAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM