private GReCaptchaResponse ParseResponseResult(string responseString) { var result = new GReCaptchaResponse(); if (_version == ReCaptchaVersion.Version2) { var resultObject = JObject.Parse(responseString); result.IsValid = resultObject.Value <bool>("success"); if (resultObject.Value <JToken>("error-codes") != null && resultObject.Value <JToken>("error-codes").Values <string>().Any()) { result.ErrorCodes = resultObject.Value <JToken>("error-codes").Values <string>().ToList(); } } return(result); }
public GReCaptchaResponse Validate() { GReCaptchaResponse result = null; var httpClient = new HttpClient(); var requestUri = string.Empty; switch (_version) { case ReCaptchaVersion.Version2: requestUri = string.Format(RECAPTCHA_VERIFY_URL_VERSION2, SecretKey, Response, RemoteIp); break; default: requestUri = string.Format(RECAPTCHA_VERIFY_URL_VERSION2, SecretKey, Response, RemoteIp); break; } try { var taskResult = httpClient.GetAsync(requestUri); taskResult.Wait(); var response = taskResult.Result; response.EnsureSuccessStatusCode(); var taskString = response.Content.ReadAsStringAsync(); taskString.Wait(); result = ParseResponseResult(taskString.Result); } catch (Exception exc) { result = new GReCaptchaResponse { IsValid = false }; result.ErrorCodes.Add("Unknown error" + exc.Message); } finally { httpClient.Dispose(); } return(result); }