![](/img/trans.png)
[英]ASP.NET C# Async - Correct solution for background HTTPRequests?
[英]Why won't my Razor, ASP.NET, C# HTTPRequests return the body of the Response?
我的最終目標是讓reCaptcha在我的Razor應用程序中運行,但我被卡住了。 我已經到了可以獲得響應標頭的地步,但不知道如何獲得實際的JSON響應體。 如果您需要更多信息,請告訴我。
在Startup.cs中----------------------------------
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
在Models> Voter.cs中----------------------------------
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Models
{
public class Voter
{
[Required(ErrorMessage = "Data is required.")]
public string reCaptchaPost { get; set; }
public string Result { get; set; }
}
}
在Vote2.cshtml.cs中----------------------------------
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using rethianIdeas.Models;
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Pages.Ideas
{
public class Vote2Model : PageModel
{
[BindProperty]
public Voter reCaptchaTest { get; set; }
[BindProperty(Name = "g-recaptcha-response")]
[Required(ErrorMessage = "No One Beats Reacaptcha!")]
public string reCaptchaG { get; set; }
public IActionResult OnGet()
{
if (reCaptchaTest == null)
{
reCaptchaTest = new Voter();
}
return Page();
}
private readonly IHttpClientFactory _clientFactory;
public Vote2Model(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=6LcUFJUUAAAAADvuNOTGONNAGETITiFgNNG0sxBg&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var Client = _clientFactory.CreateClient().SendAsync(reCaptchaRequest);
reCaptchaTest.Result = Client.Result.ToString();
}
return Page();
}
}
}
在Vote2.cshtml中----------------------------------
@page
@model rethianIdeas.Pages.Ideas.Vote2Model
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form method="post">
<div asp-validation-summary="All"></div>
<div>
<div>
<label asp-for="@Model.reCaptchaTest.reCaptchaPost"></label>
<input asp-for="@Model.reCaptchaTest.reCaptchaPost" />
<span asp-validation-for="@Model.reCaptchaTest.reCaptchaPost"></span>
</div>
</div>
<div class="g-recaptcha" data-sitekey="6LcUFJUUAAAAANOTGETTINGIT0SYj77GZHEhz73p0Q6_m"></div>
<div>
<input type="submit" value="Submit">
</div>
<div><label>Result: @Model.reCaptchaTest.Result</label></div>
</form>
返回----------------------------------
Result: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Date: Fri, 08 Mar 2019 06:02:16 GMT Cache-Control: max-age=0, private X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Server: GSE Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39" Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Expires: Fri, 08 Mar 2019 06:02:16 GMT }
我根本無法弄清楚如何從這個東西中獲取請求的主體。 請幫忙。
您正在調用異步方法但不等待( await
)它返回。
這應該讓你走正確的道路。
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=***&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(reCaptchaRequest);
if (response.IsSuccessStatusCode)
{
reCaptchaTest.Result = await response.Content.ReadAsStringAsync();
}
else
{
// Errors, do something...
}
}
return Page();
}
您可以參考ASP.NET Core Http.Client文檔以獲取更多信息。 此外, 這是一篇關於使用ASP.NET Core和reCaptcha的博客文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.