Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20529 kshitij.so 1
package com.hotspotstore.services;
2
 
3
import java.io.BufferedReader;
4
import java.io.DataOutputStream;
5
import java.io.IOException;
6
import java.io.InputStreamReader;
7
import java.io.StringReader;
8
import java.net.URL;
9
 
10
import javax.json.Json;
11
import javax.json.JsonObject;
12
import javax.json.JsonReader;
13
import javax.net.ssl.HttpsURLConnection;
14
 
15
public class VerifyRecaptcha {
16
 
17
	public static final String url = "https://www.google.com/recaptcha/api/siteverify";
18
	public static final String secret = "6LcXBA4UAAAAAC7wPMiklJ2aj2Iq1ILTjpfkhvCq";
19
	private final static String USER_AGENT = "Mozilla/5.0";
20
 
21
	public static boolean verify(String gRecaptchaResponse) throws IOException {
22
		if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
23
			return false;
24
		}
25
 
26
		try{
27
			URL obj = new URL(url);
28
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
29
 
30
			con.setRequestMethod("POST");
31
			con.setRequestProperty("User-Agent", USER_AGENT);
32
			con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
33
 
34
			String postParams = "secret=" + secret + "&response="
35
			+ gRecaptchaResponse;
36
 
37
			con.setDoOutput(true);
38
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
39
			wr.writeBytes(postParams);
40
			wr.flush();
41
			wr.close();
42
 
43
			int responseCode = con.getResponseCode();
44
			System.out.println("\nSending 'POST' request to URL : " + url);
45
			System.out.println("Post parameters : " + postParams);
46
			System.out.println("Response Code : " + responseCode);
47
 
48
			BufferedReader in = new BufferedReader(new InputStreamReader(
49
					con.getInputStream()));
50
			String inputLine;
51
			StringBuffer response = new StringBuffer();
52
 
53
			while ((inputLine = in.readLine()) != null) {
54
				response.append(inputLine);
55
			}
56
			in.close();
57
 
58
			// print result
59
			System.out.println(response.toString());
60
 
61
			//parse JSON response and return 'success' value
62
			JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
63
			JsonObject jsonObject = jsonReader.readObject();
64
			jsonReader.close();
65
 
66
			return jsonObject.getBoolean("success");
67
		}catch(Exception e){
68
			e.printStackTrace();
69
			return false;
70
		}
71
	}
72
}