網頁

Tuesday, August 16, 2011

Google reCAPTCHA 教學

reCaptcha

今天同事說他reCAPTCHA都搞不定,看他說了好幾天,我就想來試試看問題在哪,結果我開始try沒多久他就下班了(翻桌)

回正題


不知何時 reCAPTCHA 被google買去了,也許是這樣文件跟資源都還蠻多的,先到 reCAPTCHA 官網註冊取得你的 Public 跟 Private Key 後,就可以開始了,我是用沒Plugin的方式去做的,後端是 ASP.NET 去 Post 到 google 判斷是否正確,所以沒辦法直接玩到結果,等我有空來研究看看只用JavaScript能不能直接做完,不過這樣好像 Private Key 就會被看到了...

reCAPTCHA長得像這樣,用過吧。

因為我還在加班,所以直接貼code

reCAPTCHA.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="reCAPTCHA.aspx.cs" Inherits="reCAPTCHA" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src='http://www.google.com/jsapi' type='text/javascript'></script> 
<script language='javascript' type='text/javascript'>    google.load("jquery", "1.6.2");</script> 
    <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=你的Public Key">
  </script>
  <script type='text/javascript'>
      function dopost() {
          $('#<%=HiddenField1.ClientID %>').val($('#recaptcha_challenge_field').val());
          $('#<%=HiddenField2.ClientID %>').val($('#recaptcha_response_field').val());
      }
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <noscript>
        <iframe src="http://www.google.com/recaptcha/api/noscript?k=你的Public Key" height="300" width="500" frameborder="0"></iframe><br/>
        <textarea id="recaptcha_challenge_field" name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
        <input id="recaptcha_response_field" type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
    </noscript>
    <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" OnClientClick="dopost()"/>
  <br />status: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  <br />post link: <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:HiddenField ID="HiddenField2" runat="server" />
    </div>
    </form>
</body>
</html>


reCAPTCHA.aspx.cs
using System;
using System.IO;
using System.Net;
using System.Text;

public partial class reCAPTCHA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Link = "http://www.google.com/recaptcha/api/verify?privatekey=你的Private Key&remoteip=127.0.0.1&challenge=" +
            HiddenField1.Value + "&response=" + HiddenField2.Value;
        Label2.Text = Link;
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Link);
        myReq.Method = "POST";
        myReq.ContentLength = 0;
        HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
        if (myRes.StatusCode.ToString() == "OK")
        {
            Stream receiveStream = myRes.GetResponseStream();
            Encoding encode = Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(receiveStream, encode);
            Label1.Text = readStream.ReadToEnd();
        }
    }
}

Post到google的時候,要注意的東西可以看Verifying the User's Answer Without Plugins,其實就是把privatekey,remoteip,challenge,response這四個參數Post到http://www.google.com/recaptcha/api/verify,然後接回傳的值就好。


privatekey : 申請到的Private Key
remoteip : 使用者的IP
challenge : recaptcha_challenge_field 這欄位的值
response : recaptcha_response_field 這欄位的值


不知為啥我在網頁後端接不到reCAPTCHA那兩個欄位的值,所以只好用在onClientClick的時候先把值丟到HiddenField,在後端才能取到值。


若想用reCAPTCHA又不想用別人的API可以參考看看。

No comments:

Post a Comment