-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHTTPS.java
More file actions
66 lines (61 loc) · 1.64 KB
/
Copy pathHTTPS.java
File metadata and controls
66 lines (61 loc) · 1.64 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
package javaforce;
/** HTTPS Client.
*
* @author pquiring
*/
public class HTTPS extends HTTP {
public boolean open(String host) {
return open(host, 443);
}
public boolean open(String host, int port) {
this.host = host;
this.port = port;
try {
if (!super.open(host, port)) {
throw new Exception("Connect failed");
}
s = JF.connectSSL(s, KeyMgmt.getDefaultClient());
if (s == null) {
throw new Exception("SSL Upgrade failed");
}
os = s.getOutputStream();
is = s.getInputStream();
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
/** Test HTTPS */
public static void main(String[] args) {
HTTPS http = new HTTPS();
String html;
boolean print = false;
if (args.length > 0 && args[0].equals("print")) print = true;
HTTP.debug = true;
http.open("google.com");
html = http.getString("/");
http.close();
if (html == null || html.length() == 0) {
System.out.println("Error:HTTPS.get() failed");
return;
}
if (print) System.out.println(html);
http.open("www.google.com");
html = http.getString("/");
http.close();
if (html == null || html.length() == 0) {
System.out.println("Error:HTTPS.get() failed");
return;
}
if (print) System.out.println(html);
}
/** Removes user info from HTTP URL. */
public static String cleanURL(String url) {
//need to remove user:pass from url
//https://user:pass@host:port/path?opt1=val1&opt2=val2
int idx = url.indexOf('@');
if (idx == -1) return url;
return "https://" + url.substring(idx + 1);
}
}