I'm trying to do a direct post to Google using this code. I keep getting an error, "invalid private key". I have double checked it and even had someone else double check it. The reason i'm going it this way is because I'm using javascript and ajax to pass the variables to this function.
[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;
}
}