package com.ibiz.recaptcha;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.net.ssl.HttpsURLConnection;
public class VerifyUtils {
public static final String SITE_VERIFY_URL = //
"https://www.google.com/recaptcha/api/siteverify";
public static boolean verify(String gRecaptchaResponse) {
if (gRecaptchaResponse == null || gRecaptchaResponse.length() == 0) {
return false;
}
try {
URL verifyUrl = new URL(SITE_VERIFY_URL);
// Open a Connection to URL above.
HttpsURLConnection conn = (HttpsURLConnection) verifyUrl.openConnection();
// Add the Header informations to the Request to prepare send to the server.
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Data will be sent to the server.
String postParams = "secret=" + MyConstants.SECRET_KEY //
+ "&response=" + gRecaptchaResponse;
// Send Request
conn.setDoOutput(true);
// Get the output stream of Connection.
// Write data in this stream, which means to send data to Server.
OutputStream outStream = conn.getOutputStream();
outStream.write(postParams.getBytes());
outStream.flush();
outStream.close();
// Response code return from Server.
int responseCode = conn.getResponseCode();
System.out.println("responseCode=" + responseCode);
// Get the Input Stream of Connection to read data sent from the Server.
InputStream is = conn.getInputStream();
JsonReader jsonReader = Json.createReader(is);
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();
// ==> {"success": true}
System.out.println("Response: " + jsonObject);
boolean success = jsonObject.getBoolean("success");
return success;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
recaptcha.demo.zip (20个子文件)
recaptcha.demo
src
main
resources
java
com
ibiz
recaptcha
MyConstants.java 257B
VerifyUtils.java 2KB
servlet
UserInfoServlet.java 1KB
DoLoginServlet.java 2KB
LoginServlet.java 1006B
webapp
index.jsp 57B
WEB-INF
views
loginView.jsp 1KB
userInfoView.jsp 218B
web.xml 341B
.classpath 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 683B
.jsdtscope 639B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
pom.xml 2KB
.project 1KB
共 20 条
- 1
资源评论
forestdepths
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功