0

reCaptcha でこのエラーが発生します。

'Input error: response: Required field must not be blank
challenge: Required field must not be blank
privatekey: Required field must not be blank'

POST でデータを送信しているので、何が起こっているのかわかりません。これは私が使用するコードです:

    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 = new 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;
        }
    }

(もちろん、それは正しい秘密鍵ではありません:P)

何が問題なのかわかりません。データを正しく送信していると思いますが、そのエラーは明らかに何も送信していないことを示しています。

何が問題なのですか?

乾杯。

4

1 に答える 1

1

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.

于 2010-05-26T10:09:49.120 に答える