Google reCaptcha V2的VB.net实现

5

我在我的网站上无法成功通过reCaptcha验证 :(

我尝试寻找其他VB.net实现的源代码,但是没有太大的进展。以下是我已经尝试过的方法...

default.aspx.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.Script.Serialization

Public Class _Default
Inherits System.Web.UI.Page

Sub reCaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
    If (capValidate()) Then
        MsgBox("Valid Recaptcha")
    Else
        MsgBox("Not Valid Recaptcha")
    End If

End Sub

Public Function capValidate() As Boolean
    Dim Response As String = Request("g-captcha-response")
    Dim Valid As Boolean = False
    Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.google.com/recaptcha/api/siteverify?secret=THIS IS WHERE MY KEY IS&response=") & Response), HttpWebRequest)

    Try
        Using wResponse As WebResponse = req.GetResponse()

            Using readStream As New StreamReader(wResponse.GetResponseStream())
                Dim jsonResponse As String = readStream.ReadToEnd()
                Dim js As New JavaScriptSerializer()
                Dim data As MyObject = js.Deserialize(Of MyObject)(jsonResponse)

                Valid = Convert.ToBoolean(data.success)
                Return Valid
            End Using
        End Using
    Catch ex As Exception
        Return False
    End Try
End Function

Public Class MyObject
    Public Property success() As String
        Get
            Return m_success
        End Get
        Set(value As String)
            m_success = Value
        End Set
    End Property
    Private m_success As String


End Class

我的首页...

<div class="g-recaptcha" 
data-sitekey="THIS IS WHERE MY SITE KEY IS"></div>
<asp:Button ID="btnLogin" CssClass="captcha_click" runat="server" Text="Check Recaptcha" OnClick="reCaptcha_Click"  TabIndex ="4"/>

我的消息框总是返回“不是有效的reCaptcha”

有人可以解释一下为什么我无法获得有效的reCaptcha返回吗?

谢谢!


如果您从capValidate函数中删除try/catch,是否会抛出异常? - theduck
没有错误,我通过添加一个带有小错误消息的msgbox进行了检查,但它从未被调用。 - Christopher
jsonResponse 是否始终包含 success: false?还有 error-codes 字段吗? - theduck
哦,刚刚检查了一下,它总是返回false。它给了我一个错误代码:"缺少输入响应"。 - Christopher
好的,我认为你的请求可能看错了字段。请参见下面的答案。 - theduck
1个回答

6

尝试:

Dim Response As String = Request("g-recaptcha-response")

请注意re

所有的这一切都是一个拼写错误 >.< - Christopher
4年后,我能够在我的vb.net应用程序中使用OP的代码。谢谢。 - JustJohn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接