Problem with reCaptcha and .NET
I get this error with reCaptcha:
'Input error: response: Required field must not be blank
challenge: Required field must not be blank
privatekey: Required field must not be blank'
I'm sending the data via POST, so I don't understand what is going on. This is the code I use:
public static Boolean Check(String challenge, String response)
{
try
{
String privatekey = "7LeAbLoSAAAABJBn05uo6sZoFNoFnK2XKyF3dRXL";
String remoteip = HttpContext.Current.Request.UserHostAddress;
WebRequest req = WebRequest.Create("http://api-verify.recaptcha.net/verify");
req.Method = "POST";
using (StreamWriter sw = ne开发者_StackOverfloww StreamWriter(req.GetRequestStream()))
{
sw.Write("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privatekey, remoteip, challenge, response);
sw.Flush();
}
String resultString = String.Empty;
String errorString = String.Empty;
using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
{
resultString = sr.ReadLine();
errorString = sr.ReadLine();
}
Boolean b;
return Boolean.TryParse(resultString, out b) && b;
}
catch (Exception)
{
return false;
}
}
(Of course that'is not the correct private key :P)
I have no idea what the problem is about, I think I'm sending the data correctly, but that error says that apparently I'm not sending anything.
What could be the problem?
Cheers.
Great, I forgot how to do a POST properly ¬¬'
public static Boolean Check(String challenge, String response)
{
try
{
String privatekey = "7LeAbLoSAAAABJBn05uo6sZoFNoFnK2XKyF3dRXL";
String remoteip = HttpContext.Current.Request.UserHostAddress;
WebRequest req = WebRequest.Create("http://api-verify.recaptcha.net/verify");
req.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privatekey, remoteip, challenge, response));
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = byteArray.Length;
req.GetRequestStream().Write(byteArray, 0, byteArray.Length);
String resultString = String.Empty;
String errorString = String.Empty;
using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
{
resultString = sr.ReadLine();
errorString = sr.ReadLine();
}
Boolean b;
return Boolean.TryParse(resultString, out b) && b;
}
catch (Exception)
{
return false;
}
}
It's working now.
精彩评论