我正在尝试使用此代码直接向 Google 发帖。我不断收到错误消息,“无效的私钥”。我已经仔细检查了它,甚至让其他人仔细检查了它。我这样做的原因是因为我使用 javascript 和 ajax 将变量传递给这个函数。
[HttpPost]
public string ValidateReCaptcha(string captchaChallenge, string captchaResponse)
{
if (captchaChallenge != null && captchaResponse != null)
{
string strPrivateKey = System.Web.Configuration.WebConfigurationManager.AppSettings["recaptchaPrivateKey"].ToString();
string strParameters = "?privatekey=" + strPrivateKey +
"&remoteip=" + HttpContext.Request.UserHostAddress.ToString() +
"&challenge=" + captchaChallenge +
"&response=" + captchaResponse;
WebRequest request = WebRequest.Create("http://www.google.com/recaptcha/api/verify");
request.Method = "POST";
string postData = strParameters;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
if (responseFromServer.ToString() != "true")
{
errorCodeList.Add(8);
return responseFromServer + strPrivateKey;
}
else
{
return responseFromServer;
}
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
else
{
errorCodeList.Add(8);
return null;
}
}