-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVehicleNumberChecker.java
More file actions
139 lines (123 loc) · 6.58 KB
/
VehicleNumberChecker.java
File metadata and controls
139 lines (123 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.client.utils.URLEncodedUtils;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The class uses Indian Govt's website https://parivahan.gov.in to check vehicle number and retrieve information
*
* Reference Code: https://gist.github.com/githubsrinath/560e2382cb3f84e421f8cf14e5e912b0#file-03-java-or-android
*
*
*/
public class VehicleNumberChecker {
public static void main(String[] args) {
// Must be 2 letters and characters only (Union Territory / State code)
String utOrState = "XX";
// Must be 2 letters and numbers only (District serial number)
String districtNumber = "00";
// optional and can be blank (Optional code for additional sub-category needs)
String additionalCode = "XX";
// Must be 4 letters and numbers only (Vehicle serial number)
String serialNumber = "0000";
Map<String, String> data = getData(utOrState + districtNumber + additionalCode + serialNumber);
JSONObject json = new JSONObject(data);
System.out.println(json);
}
private static Map<String, String> getData(String str) {
Map<String, String> resultMap = new HashMap<>();
String str2 = "";
Matcher matcher = Pattern.compile("(\\w{2})(\\d{2})(\\D*?)(\\d{1,4})").matcher(str);
if (matcher.matches()) {
str2 = matcher.replaceFirst("$1$2$3-$4");
}
String[] split = str2.split("-");
String str3 = split[0];
str = split[1];
try {
Connection.Response execute = Jsoup.connect("https://parivahan.gov.in/rcdlstatus/?pur_cd=102")
//.validateTLSCertificates(false) // The site is secure now and it is expected to always validate cert
.followRedirects(true)
.ignoreHttpErrors(true)
.method(Connection.Method.GET).execute();
if (execute.statusCode() <= HttpStatus.SC_INTERNAL_SERVER_ERROR) {
Map<String, String> cookies = execute.cookies();
Document parse = Jsoup.parse(execute.body());
Element first = parse.getElementsByAttributeValue("name", "javax.faces.ViewState").first();
if (first == null) {
first = parse.getElementById("j_id1:javax.faces.ViewState:0");
}
String attr = first.attr("value");
String trim = Jsoup.parse(execute.body())
.getElementsByAttributeValueStarting("id", "form_rcdl:j_idt")
.select("button")
.get(0).attr("id").trim();
str = Jsoup.connect("https://parivahan.gov.in/rcdlstatus/vahan/rcDlHome.xhtml")
//.validateTLSCertificates(false) // The site is secure now and it is expected to always validate cert
.followRedirects(true)
.method(Connection.Method.POST)
.cookies(cookies)
.referrer("https://parivahan.gov.in/rcdlstatus/?pur_cd=102")
.header("Content-Type", URLEncodedUtils.CONTENT_TYPE)
.header("Host", "parivahan.gov.in")
.header(HttpHeaders.ACCEPT, "application/xml, text/xml, */*; q=0.01")
.header(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.5")
.header(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br")
.header("X-Requested-With", "XMLHttpRequest")
.header("Faces-Request", "partial/ajax")
.header("Origin", "https://parivahan.gov.in")
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36")
.data("javax.faces.partial.ajax", "true")
.data("javax.faces.source", trim)
.data("javax.faces.partial.execute", "@all")
.data("javax.faces.partial.render", "form_rcdl:pnl_show form_rcdl:pg_show form_rcdl:rcdl_pnl")
.data(trim, trim)
.data("form_rcdl", "form_rcdl")
.data("form_rcdl:tf_reg_no1", str3)
.data("form_rcdl:tf_reg_no2", str)
.data("javax.faces.ViewState", attr)
.execute()
.body();
if (str.contains("Registration No. does not exist!!!")) {
resultMap.put(str, "No Record(s) Found");
} else {
String htmlString = "<!DOCTYPE html><html><body>" +
str.substring(str.indexOf("<table"), str.lastIndexOf("</table>")) +
"</body></html>";
Document parse2 = Jsoup.parse(htmlString);
Element first2 = parse2.select("table").first();
if (first2 != null) {
Elements select = first2.select("tr");
for (Element element : select) {
Elements select2 = element.select("td");
if (select2.size() == 2) {
String key = select2.get(0).text().replace(":", "");
resultMap.put(key, select2.get(1).text());
} else if (select2.size() == 4) {
String key = select2.get(0).text().replace(":", "");
resultMap.put(key, select2.get(1).text());
key = select2.get(2).text().replace(":", "");
resultMap.put(key, select2.get(3).text());
}
}
} else {
resultMap.put(str, "No Record(s) Found");
}
}
} else {
resultMap.put(str, "500 Server Error");
}
} catch (Exception unused) {
resultMap.put(str, "Server Error");
}
return resultMap;
}
}