2
如何在Windows Phone上顯示Recaptcha?Recaptcha和Windows Phone
我在後端使用Windows Phone 8.1和ASP.NET WebApi。
我的WebApi方法需要一些參數中的數據,recaptcha-challenge和recaptcha-response。
[HttpPost]
public void Method(MyData data, string challenge, string response)
{
string recaptchaValidationUrl = "http://www.google.com/recaptcha/api/verify";
string privateKey = "MY_RECAPTCHA_PRIVATE_KEY";
if (!CheckRecaptcha(recaptchaValidationUrl, request.RcChallenge, request.RcResponse, privateKey,
HttpContext.Current.Request.UserHostAddress))
{
return new FindTechResResponse { ErrorMessage = "Invalid captcha code" };
}
}
public static bool CheckRecaptcha(string recaptchaValidationUrl, string challenge, string response, string privateKey, string clientIp)
{
using (WebClient webClient = new WebClient())
{
NameValueCollection data = new NameValueCollection();
data["privatekey"] = privateKey;
data["remoteip"] = clientIp;
data["challenge"] = challenge;
data["response"] = response;
byte[] responseBytes = webClient.UploadValues(recaptchaValidationUrl, "POST", data);
string responseString = Encoding.UTF8.GetString(responseBytes);
string isSolvedString = responseString.Split('\n')[0];
bool isSolved = bool.Parse(isSolvedString);
return isSolved;
}
}
}