2010-05-26 65 views
0

我得到這個錯誤與驗證碼:問題的驗證碼和.NET

'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; 
     } 
    } 

(當然that'is不正確的私有密鑰:P)

我不知道問題是什麼,我想我發送數據正確,但那個錯誤說,顯然我沒有發送任何東西。

可能是什麼問題?

乾杯。

+1

爲什麼不使用您在http://code.google.com/p/recaptcha上找到的社區支持的開源實現,而不是編寫自己的實現。 – 2010-05-26 13:58:45

+0

因爲使用插件是非常簡單的事情:D .NET的插件是用於ASP.NET的,我沒有使用ASP.NET。 無論如何,我喜歡自己做這些事情:P – vtortola 2010-05-26 14:17:48

回答

1

太好了,我忘了怎麼做一個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"; 

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

它的工作現在。

我真的不知道如何關閉這個quesion,如果有管理員看到這個......請關閉它:d

乾杯。